【发布时间】:2017-12-12 19:15:30
【问题描述】:
我试图创建一个卷积自动编码器,但我遇到了问题 代码如下:
import tensorflow as tf
import numpy as np
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
img = mpimg.imread('data.jpg')
x = (img-np.mean(img))/np.std(img)
y = img
epochs = 500
def autoencoder(x, weights):
global output
output = tf.nn.conv2d([x], weights[0], strides=[1,1,1,1],padding='SAME')
output = tf.nn.relu(output)
output = tf.nn.conv2d(output, weights[1], strides=[1,2,2,1],padding='SAME')
output = tf.nn.relu(output)
output = tf.nn.conv2d(output, weights[2], strides=[1,2,2,1],padding='SAME')
output = tf.nn.relu(output)
output = tf.nn.conv2d(output, weights[3], strides=[1,2,2,1],padding='SAME')
output = tf.nn.relu(output)
output = tf.nn.conv2d(output, weights[4], strides=[1,1,1,1],padding='SAME')
output = tf.nn.relu(output)
output = tf.image.resize_images(output, [50, 38])
output = tf.nn.conv2d(output, weights[5], strides=[1,1,1,1],padding='SAME')
output = tf.nn.relu(output)
output = tf.image.resize_images(output, [100, 76])
output = tf.nn.conv2d(output, weights[6], strides=[1,1,1,1],padding='SAME')
output = tf.nn.relu(output)
output = tf.image.resize_images(output, [200, 152])
output = tf.nn.conv2d(output, weights[7], strides=[1,1,1,1],padding='SAME')
weights = [tf.Variable(tf.random_normal([5,5,3,3])),
tf.Variable(tf.random_normal([5,5,3,3])),
tf.Variable(tf.random_normal([5,5,3,3])),
tf.Variable(tf.random_normal([5,5,3,3])),
tf.Variable(tf.random_normal([5,5,3,3])),
tf.Variable(tf.random_normal([5,5,3,3])),
tf.Variable(tf.random_normal([5,5,3,3])),
tf.Variable(tf.random_normal([5,5,3,3]))]
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for e in range(epochs):
print('epoch:',e+1)
autoencoder(tf.cast(x,tf.float32), weights)
plt.imshow(output.eval()[0])
plt.savefig(str(e+1)+'.png')
cost = tf.reduce_mean(tf.reduce_mean(tf.squared_difference(output.eval()[0],y)))
tf.train.AdamOptimizer().minimize(cost)
这是错误:
Traceback (most recent call last):
File "D:\Kay\Tensorflow\Session 3\Autoencoder.py", line 56, in <module>
tf.train.AdamOptimizer().minimize(cost)
File "C:\Users\Katharina\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\training\optimizer.py", line 276, in minimize
([str(v) for _, v in grads_and_vars], loss))
ValueError: No gradients provided for any variable, check your graph for ops that do not support gradients, between variables ['Tensor("Variable/read:0", shape=(5, 5, 3, 3), dtype=float32)', 'Tensor("Variable_1/read:0", shape=(5, 5, 3, 3), dtype=float32)', 'Tensor("Variable_2/read:0", shape=(5, 5, 3, 3), dtype=float32)', 'Tensor("Variable_3/read:0", shape=(5, 5, 3, 3), dtype=float32)', 'Tensor("Variable_4/read:0", shape=(5, 5, 3, 3), dtype=float32)', 'Tensor("Variable_5/read:0", shape=(5, 5, 3, 3), dtype=float32)', 'Tensor("Variable_6/read:0", shape=(5, 5, 3, 3), dtype=float32)', 'Tensor("Variable_7/read:0", shape=(5, 5, 3, 3), dtype=float32)'] and loss Tensor("Mean_1:0", shape=(), dtype=float32).
谁能帮帮我?
【问题讨论】:
-
如果答案解决了您的问题,您应该接受它
标签: python tensorflow neural-network autoencoder