https://www.quora.com/How-do-I-implement-a-1D-Convolutional-autoencoder-in-Keras-for-numerical-dataset

    # 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

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')])

貌似上面的有问题,修改后是:

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

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
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
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
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):

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

conv1d UpSampling1D aotoencoder 自编码代码摘录的更多相关文章

  1. live Templates 活动模板. 配置完之后,就可以快速编码-代码块

    配置:live Templates 活动模板. 配置完之后,就可以快速编码-代码块. 输入startflask敲回车:   就会生成代码:   怎么做到的呢? 如下:   注意第七步: 原本不是cha ...

  2. wndows程序设计之书籍知识与代码摘录-封装一个类似printf的messagebox

    //----------------------------------------- //本程序展示了如何实现MessageBoxPrintf函数 //本函数能像printf那样格式化输出 //摘录 ...

  3. wndows程序设计之书籍知识与代码摘录-获取视屏显示器像素等参数GetsystemMetrics

    以下的代码段用于获取视屏显示器的高度宽度,以像素为单位. int sxScreen, cyScreen; cxScreen = GetSystemMetrics (SM_CXSCREEN); cySc ...

  4. ISD9160学习笔记04_ISD9160音频编码代码分析

    前言 录音例程涉及了录音和播放两大块内容,上篇笔记说了播放,这篇就来说说录音这块,也就是音频编码这部分功能. 上篇笔记中的这段话太装逼了,我决定再复制下,嘿嘿. “我的锤子便签中有上个月记下的一句话, ...

  5. src或者href值为base64编码代码

    大家可能注意到了,网页上有些图片的src或css背景图片的url后面跟了一大串字符,比如:data:image/png;base64, iVBORw0KGgoAAAANSUhEUgnZVJlYWR5c ...

  6. PYTHON代码摘录

    文件处理 #典型的读取文件代码 row_data = {} with open('PaceData.csv') as paces: column_heading = paces.readline(). ...

  7. PERL代码摘录

    1. 语法与数据结构 #嵌套哈希的赋值和取值 $HashTable{$key} = [@Array] #这个是赋值 @Array = @{ $HashTable{$key} } # 这个是取值 #Pe ...

  8. 优秀代码摘录片段一:LinkedList中定位index时使用折半思想

    在LinkedList有一段小代码,实现的功能是,在链表中间进行插如,所以在插如的过程中会需要找到对应的index位置的node元素: 如果放在平时只为了实现功能而进行遍历查找,很多人会直接使用一个w ...

  9. Javascript代码摘录

    判断浏览器窗口高度 if (document.documentElement.clientHeight <800) { var elm = document.getElementById('Di ...

随机推荐

  1. Jsoup解析网页html

    Jsoup解析网页html 解析网页demo: 利用Jsoup获取截图中的数据信息: html代码片段: <!-- 当前基金档案\计算\定投\开户 start --> <div cl ...

  2. 前端开发环境全面配置 --- mac OS

    Mac 开发配置 brew /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install ...

  3. noip 邮票面值设计 - 搜索 - 动态规划

    描述 给定一个信封,最多只允许粘贴N张邮票,计算在给定M(N+M<=10)种邮票的情况下(假定所有的邮票数量都足够),如何设计邮票的面值,能得到最大max ,使得1-max之间的每一个邮资值都能 ...

  4. Duilib应用修改程序图标方法(转载)

    转载:http://www.cnblogs.com/lanzhi/p/6468596.html 本文向大家介绍如何修改duilib应用图标,对于win32或者mfc应用来说,我们可以在注册窗口类时指定 ...

  5. Metasploit应用举例

    本篇文章包含以下几方面内容: 1.Metasploit端口扫描: 2.用其他模块 3.metasploit smb获取系统信息 4.Metsploit服务识别 5.ftp识别: 6.metasploi ...

  6. I2C总线信号时序总结【转】

    本文转载自:https://i.cnblogs.com/EditPosts.aspx?opt=1 I2C总线信号时序总结 总线空闲状态  I2C总线总线的SDA和SCL两条信号线同时处于高电平时,规定 ...

  7. 三点估算和PERT技术

    三点估算是PMP考试中的必考题目,每次约2-4道题目.现在就三点估算和PERT技术做详细讲解,以飨读者. 通过考虑估算中的不确定性和风险,可以提高活动持续时间估算的准确性.这个概念起源于计划评审技术( ...

  8. 帮助大家理解java中的随机和继承,动态绑定.

    package com.ykmimi.javabianchengsixiang; /** * 形状的继承 随机形状生成器 * @author ukyor */ import java.util.Ran ...

  9. PTA第三次上机

    5-1 #include <iostream> #include <cstdlib> #include <string.h> using namespace std ...

  10. UVa 10163 仓库守卫

    https://vjudge.net/problem/UVA-10163 题意: 有n个仓库,m个管理员,每个管理员有一个能力值P(接下来的一行有m个数,表示每个管理员的能力值) 每个仓库只能由一个管 ...