我正在尝试为超分辨率CNN实现一个类。调用训练函数时出现错误
SRCNN/srcnn.py", line 100, in train
epoch_loss += c/(21.0*21.0)
TypeError: unsupported operand type(s) for /: 'NoneType' and 'float'
基本上,sess.run()在执行Losses变量后返回None。下面是用于网络体系结构的代码,检查了在占位符X和Y中传递的数据后,我确定问题不是由数据输入引起的。
class srcnn:
def __init__(self):
self.graph = self.build_graph()
with self.graph.as_default():
self.saver=tf.train.Saver(max_to_keep=4)
def build_graph(self):
graph = tf.Graph()
with graph.as_default():
x = tf.placeholder(tf.float32, shape=[None, 33, 33, 3], name='X')
y = tf.placeholder(tf.float32, shape=[None, 21, 21, 3], name='Y')
predictions = self.model(x)
Losses = self.loss(predictions, y)
optimization = self.optimize(Losses)
return graph
def model(self, features):
input_layer = tf.reshape(features, [-1, 33, 33, 3])
#Patch extraction and representation
conv1 = tf.layers.conv2d(inputs=input_layer, filters=64, kernel_size=[9,9], padding='valid', activation=tf.nn.relu)
#Non-linear mapping
conv2 = tf.layers.conv2d(inputs=conv1, filters=32, kernel_size=[1,1], activation=tf.nn.relu)
#Reconstruction
conv3 = tf.layers.conv2d(inputs=conv2, filters=3, kernel_size=[5,5], activation=tf.nn.relu, name='ouput')
return conv3
def loss(self, predictions, y):
loss = tf.reduce_sum(tf.square(predictions-y), name="Loss")
return loss
def optimize(self, loss):
optimizer = tf.train.AdamOptimizer()
return optimizer.minimize(loss)
def train(self, dataset, num_epochs, batch_size=None, checkpoints_after=None):
global_step_count = 0
with tf.Session(graph=self.graph) as sess:
sess.run(tf.global_variables_initializer())
global_step_count = 0
optimizer = self.graph.get_operation_by_name('Adam')
losses=self.graph.get_operation_by_name('Loss')
for i in range(num_epochs):
epoch_loss = 0
count = 0
for data, label in dataset.get_next_batch(batch_size):
_, c = sess.run([optimizer, losses], feed_dict={'X:0':data, 'Y:0':label})
print(c)
epoch_loss += c/(21.0*21.0)
count += 1
global_step_count += 1
epoch_loss = epoch_loss/count
print('Epoch: ', i+1, ' Epoch_loss: ', epoch_loss)