如何在自动编码器中拆分编码器和解码器?

时间:2019-09-01 22:54:08

标签: python tensorflow keras conv-neural-network autoencoder

我正在尝试拆分自动编码器,但是每当尝试尝试时,都会出现此错误,并且我不知道为什么会得到它。我正在Windows 10上的python 3.7 64bit中运行此程序。

inp = open('train.csv',"rb")
X = pickle.load(inp)
X = X/255.0
X = np.array(X)
X = np.reshape(X,(-1,x,y,h))

DATA = "C:/Users/dalto/Documents/geo4/dataset"
dataout = r"C:\Users\dalto\Documents\geo4\outset\video"


input_img = keras.layers.Input(shape = (x, y, h))

#make encoder
x = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)

encoder = Model(input_img, encoded)

encoder.summary()

#make decoder
decoder_input= Input(shape = (32,16,32))

decoder = Conv2D(32, (3, 3), activation='relu', padding='same')(decoder_input)
x = UpSampling2D((2, 2))(decoder)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)

decoder = Model(decoder_input, decoded)

#combine them both
auto_input = Input(shape=(x, y, h))
encoded = encoder(auto_input)
decoded = decoder(encoded)

autoencoder = Model(auto_input, decoded)
autoencoder.compile(loss='mean_squared_error', optimizer = 
keras.optimizers.RMSprop())
#train
autoencoder_train = autoencoder.fit(X, X, 
batch_size=batch_size,epochs=epochs,verbose=1

我希望能够对某些内容进行编码并保存编码后的文件,然后在其上运行解码器以获取放大的文件。这是错误:

Traceback (most recent call last):
  File "C:\Users\dalto\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\eager\execute.py", line 146, in make_shape
    shape = tensor_shape.as_shape(v)
  File "C:\Users\dalto\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 1211, in as_shape
    return TensorShape(shape)
  File "C:\Users\dalto\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 781, in __init__
self._dims = [as_dimension(d) for d in dims_iter]
File "C:\Users\dalto\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 781, in <listcomp>
    self._dims = [as_dimension(d) for d in dims_iter]
  File "C:\Users\dalto\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 723, in as_dimension
return Dimension(value)
  File "C:\Users\dalto\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 192, in __init__
self._value = int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'Tensor'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
   File "C:\Users\dalto\Documents\geo4\train.py", line 54, in <module>
     auto_input = Input(shape=(x, y, h))
   File "C:\Users\dalto\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\input_layer.py", line 256, in Input
input_tensor=tensor)
  File "C:\Users\dalto\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\input_layer.py", line 123, in __init__
    name=self.name)
   File "C:\Users\dalto\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\backend.py", line 1001, in placeholder
     x = array_ops.placeholder(dtype, shape=shape, name=name)
   File "C:\Users\dalto\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\ops\array_ops.py", line 2129, in placeholder
return gen_array_ops.placeholder(dtype=dtype, shape=shape, name=name)
   File "C:\Users\dalto\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 7398, in placeholder
     shape = _execute.make_shape(shape, "shape")
  File "C:\Users\dalto\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\eager\execute.py", line 148, in make_shape
raise TypeError("Error converting %s to a TensorShape: %s." % (arg_name, e))
TypeError: Error converting shape to a TensorShape: int() argument must be a string, a bytes-like object or a number, not 'Tensor'.

0 个答案:

没有答案