conv1d UpSampling1D aotoencoder 自编码代码摘录
https://www.quora.com/How-do-I-implement-a-1D-Convolutional-autoencoder-in-Keras-for-numerical-dataset
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | # ENCODER input_sig = Input (batch_shape = ( None , 128 , 1 )) x = Conv1D( 64 , 3 , activation = 'relu' , padding = 'valid' )(input_sig) x1 = MaxPooling1D( 2 )(x) x2 = Conv1D( 32 , 3 , activation = 'relu' , padding = 'valid' )(x1) x3 = MaxPooling1D( 2 )(x2) flat = Flatten()(x3) encoded = Dense( 32 ,activation = 'relu' )(flat) print ( "shape of encoded {}" . format (K.int_shape(encoded))) # DECODER x2_ = Conv1D( 32 , 3 , activation = 'relu' , padding = 'valid' )(x3) x1_ = UpSampling1D( 2 )(x2_) x_ = Conv1D( 64 , 3 , activation = 'relu' , padding = 'valid' )(x1_) upsamp = UpSampling1D( 2 )(x_) flat = Flatten()(upsamp) decoded = Dense( 128 ,activation = 'relu' )(flat) decoded = Reshape(( 128 , 1 ))(decoded) print ( "shape of decoded {}" . format (K.int_shape(decoded))) autoencoder = Model(input_sig, decoded) autoencoder. compile (optimizer = 'adam' , loss = 'mse' , metrics = [ 'accuracy' ]) |
http://qaru.site/questions/418926/keras-autoencoder-error-when-checking-target
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | from keras.layers import Input , Dense, Conv1D, MaxPooling1D, UpSampling1D from keras.models import Model from keras import backend as K import scipy as scipy import numpy as np mat = scipy.io.loadmat( 'edata.mat' ) emat = mat[ 'edata' ] input_img = Input (shape = ( 64 , 1 )) # adapt this if using `channels_first` image data format x = Conv1D( 32 , ( 9 ), activation = 'relu' , padding = 'same' )(input_img) x = MaxPooling1D(( 4 ), padding = 'same' )(x) x = Conv1D( 16 , ( 9 ), activation = 'relu' , padding = 'same' )(x) x = MaxPooling1D(( 4 ), padding = 'same' )(x) x = Conv1D( 8 , ( 9 ), activation = 'relu' , padding = 'same' )(x) encoded = MaxPooling1D( 4 , padding = 'same' )(x) x = Conv1D( 8 , ( 9 ), activation = 'relu' , padding = 'same' )(encoded) x = UpSampling1D(( 4 ))(x) x = Conv1D( 16 , ( 9 ), activation = 'relu' , padding = 'same' )(x) x = UpSampling1D(( 4 ))(x) x = Conv1D( 32 , ( 9 ), activation = 'relu' )(x) x = UpSampling1D(( 4 ))(x) decoded = Conv1D( 1 , ( 9 ), activation = 'sigmoid' , padding = 'same' )(x) autoencoder = Model(input_img, decoded) autoencoder. compile (optimizer = 'adadelta' , loss = 'binary_crossentropy' ) x_train = emat[:, 0 : 80000 ] x_train = np.reshape(x_train, (x_train.shape[ 1 ], 64 , 1 )) x_test = emat[:, 80000 : 120000 ] x_test = np.reshape(x_test, (x_test.shape[ 1 ], 64 , 1 )) from keras.callbacks import TensorBoard autoencoder.fit(x_train, x_train, epochs = 50 , batch_size = 128 , shuffle = True , validation_data = (x_test, x_test), callbacks = [TensorBoard(log_dir = '/tmp/autoencoder' )]) |
貌似上面的有问题,修改后是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | input_img = Input (shape = ( 64 , 1 )) x = Conv1D( 32 , ( 9 ), activation = 'relu' , padding = 'same' )(input_img) x = MaxPooling1D(( 4 ), padding = 'same' )(x) x = Conv1D( 16 , ( 9 ), activation = 'relu' , padding = 'same' )(x) x = MaxPooling1D(( 4 ), padding = 'same' )(x) x = Conv1D( 8 , ( 9 ), activation = 'relu' , padding = 'same' )(x) encoded = MaxPooling1D( 4 , padding = 'same' )(x) x = Conv1D( 8 , ( 9 ), activation = 'relu' , padding = 'same' )(encoded) x = UpSampling1D(( 4 ))(x) x = Conv1D( 16 , ( 9 ), activation = 'relu' , padding = 'same' )(x) x = UpSampling1D(( 4 ))(x) x = Conv1D( 32 , ( 9 ), activation = 'relu' )(x) x = UpSampling1D(( 8 ))(x) ## <-- change here (was 4) decoded = Conv1D( 1 , ( 9 ), activation = 'sigmoid' , padding = 'same' )(x) autoencoder = Model(input_img, decoded) autoencoder. compile (optimizer = 'adadelta' , loss = 'binary_crossentropy' ) |
一些生成模型:https://www.programcreek.com/python/example/93306/keras.layers.convolutional.UpSampling1D
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | def generator_model(): # CDNN Model print (INPUT_LN, N_GEN_l, CODE_LN) model = Sequential() model.add(Convolution1D( 16 , 5 , border_mode = 'same' , input_shape = (CODE_LN, 1 ))) model.add(Activation( 'relu' )) model.add(UpSampling1D(length = N_GEN_l[ 0 ])) model.add(Convolution1D( 32 , 5 , border_mode = 'same' )) model.add(Activation( 'relu' )) model.add(UpSampling1D(length = N_GEN_l[ 1 ])) model.add(Convolution1D( 1 , 5 , border_mode = 'same' )) model.add(Activation( 'tanh' )) return model |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | def generator_model(noise_dim = 100 , aux_dim = 47 , model_name = "generator" ): # Merge noise and auxilary inputs gen_input = Input (shape = (noise_dim,), name = "noise_input" ) aux_input = Input (shape = (aux_dim,), name = "auxilary_input" ) x = concatenate([gen_input, aux_input], axis = - 1 ) # Dense Layer 1 x = Dense( 10 * 100 )(x) x = BatchNormalization()(x) x = LeakyReLU( 0.2 )(x) # output shape is 10*100 # Reshape the tensors to support CNNs x = Reshape(( 100 , 10 ))(x) # shape is 100 x 10 # Conv Layer 1 x = Conv1D(filters = 250 , kernel_size = 13 , padding = 'same' )(x) x = BatchNormalization()(x) x = LeakyReLU( 0.2 )(x) # output shape is 100 x 250 x = UpSampling1D(size = 2 )(x) # output shape is 200 x 250 # Conv Layer 2 x = Conv1D(filters = 100 , kernel_size = 13 , padding = 'same' )(x) x = BatchNormalization()(x) x = LeakyReLU( 0.2 )(x) # output shape is 200 x 100 x = UpSampling1D(size = 2 )(x) # output shape is 400 x 100 # Conv Layer 3 x = Conv1D(filters = 1 , kernel_size = 13 , padding = 'same' )(x) x = BatchNormalization()(x) x = Activation( 'tanh' )(x) # final output shape is 400 x 1 generator_model = Model( outputs = [x], inputs = [gen_input, aux_input], name = model_name) return generator_model |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | def generator_model_44(): # CDNN Model model = Sequential() model.add(Convolution1D( 16 , 5 , border_mode = 'same' , input_shape = (CODE_LN, 1 ))) model.add(Activation( 'relu' )) model.add(UpSampling1D(length = 4 )) model.add(Convolution1D( 32 , 5 , border_mode = 'same' )) model.add(Activation( 'relu' )) model.add(UpSampling1D(length = 4 )) model.add(Convolution1D( 1 , 5 , border_mode = 'same' )) # model.add(Activation('relu')) return model |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | def generator_model(noise_dim = 100 , aux_dim = 47 , model_name = "generator" ): # Merge noise and auxilary inputs gen_input = Input (shape = (noise_dim,), name = "noise_input" ) aux_input = Input (shape = (aux_dim,), name = "auxilary_input" ) x = merge([gen_input, aux_input], mode = "concat" , concat_axis = - 1 ) # Dense Layer 1 x = Dense( 10 * 100 )(x) x = BatchNormalization()(x) x = LeakyReLU( 0.2 )(x) # output shape is 10*100 # Reshape the tensors to support CNNs x = Reshape(( 100 , 10 ))(x) # shape is 100 x 10 # Conv Layer 1 x = Convolution1D(nb_filter = 250 , filter_length = 13 , border_mode = 'same' , subsample_length = 1 )(x) x = BatchNormalization()(x) x = LeakyReLU( 0.2 )(x) # output shape is 100 x 250 x = UpSampling1D(length = 2 )(x) # output shape is 200 x 250 # Conv Layer 2 x = Convolution1D(nb_filter = 100 , filter_length = 13 , border_mode = 'same' , subsample_length = 1 )(x) x = BatchNormalization()(x) x = LeakyReLU( 0.2 )(x) # output shape is 200 x 100 x = UpSampling1D(length = 2 )(x) # output shape is 400 x 100 # Conv Layer 3 x = Convolution1D(nb_filter = 1 , filter_length = 13 , border_mode = 'same' , subsample_length = 1 )(x) x = BatchNormalization()(x) x = Activation( 'tanh' )(x) # final output shape is 400 x 1 generator_model = Model( input = [gen_input, aux_input], output = [x], name = model_name) return generator_model |
下面代码有问题,但是结构可以参考(https://groups.google.com/forum/#!topic/keras-users/EXOuZ4YKONY):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | import numpy as np from keras.layers import Input # define the input shape for the model from keras.layers import Conv1D, MaxPooling1D, UpSampling1D # for the convnet structure from keras.models import Model # for the overall definition from keras.initializers import Constant # bias initialisation from keras.initializers import TruncatedNormal # kernel initialissation from keras.layers.advanced_activations import LeakyReLU # activation function (from NSynth) # define input shape input_img = Input (shape = ( 500 , 128 )) print ( 'Some information about tensor expected shapes' ) print ( 'Input tensor shape:' , input_img.shape) # define encoder convnet # obs: 1D convolution implemented x = Conv1D(filters = 128 ,kernel_size = 4 ,activation = LeakyReLU(),padding = 'causal' ,dilation_rate = 4 ,bias_initializer = Constant( 0.1 ),kernel_initializer = TruncatedNormal())(input_img) x = Conv1D(filters = 256 ,kernel_size = ( 4 ),activation = LeakyReLU(),padding = 'causal' ,dilation_rate = 2 ,bias_initializer = Constant( 0.1 ),kernel_initializer = TruncatedNormal())(x) x = MaxPooling1D(pool_size = 4 ,strides = 4 )(x) encoded = Conv1D(filters = 512 ,kernel_size = 4 ,activation = LeakyReLU(),padding = 'causal' ,bias_initializer = Constant( 0.1 ),kernel_initializer = TruncatedNormal())(x) print ( 'Encoded representation tensor shape:' , encoded.shape) # define decoder convnet x = Conv1D(filters = 256 ,kernel_size = 4 ,activation = LeakyReLU(),padding = 'causal' ,bias_initializer = Constant( 0.1 ),kernel_initializer = TruncatedNormal())(encoded) x = UpSampling1D(size = 4 )(x) x = Conv1D(filters = 128 ,kernel_size = 4 ,activation = LeakyReLU(),padding = 'causal' ,dilation_rate = 2 ,bias_initializer = Constant( 0.1 ),kernel_initializer = TruncatedNormal())(x) decoded = Conv1D(filters = 1 ,kernel_size = 4 ,activation = LeakyReLU(),padding = 'causal' ,dilation_rate = 4 ,bias_initializer = Constant( 0.1 ),kernel_initializer = TruncatedNormal())(x) print ( 'Decoded representation tensor shape:' , decoded.shape) # define overal autoencoder model cae = Model(inputs = input_img, outputs = decoded) cae. compile (optimizer = 'adam' , loss = 'mse' ) # check for equal size # obs: the missing value is the batch_size if input_img.shape[ 1 :] ! = decoded.shape[ 1 :]: print ( 'alert: in/out dimension mismatch' ) And, with no surprise, I get alert: in / out dimension mismatch |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· 程序员常用高效实用工具推荐,办公效率提升利器!
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 【译】WinForms:分析一下(我用 Visual Basic 写的)
2017-10-31 查看spark是否有僵尸进程,有的话,先杀掉。可以使用下面命令