Python tensorflow,训练后拆分自动编码器
我有tensorflow 1x中的自动编码器模型(不是keras),我正在尝试在培训后将模型拆分为编码器和解码器 两者在同一范围内起作用 我有3个占位符Python tensorflow,训练后拆分自动编码器,python,tensorflow,autoencoder,Python,Tensorflow,Autoencoder,我有tensorflow 1x中的自动编码器模型(不是keras),我正在尝试在培训后将模型拆分为编码器和解码器 两者在同一范围内起作用 我有3个占位符 self.X = tf.placeholder(shape=[None, vox_res64, vox_res64, vox_res64, 1], dtype=tf.float32) self.Z = tf.placeholder(shape=[None,500], dtype=tf.float32) self.Y = tf.placehol
self.X = tf.placeholder(shape=[None, vox_res64, vox_res64, vox_res64, 1], dtype=tf.float32)
self.Z = tf.placeholder(shape=[None,500], dtype=tf.float32)
self.Y = tf.placeholder(shape=[None, vox_rex256, vox_rex256, vox_rex256, 1], dtype=tf.float32)
with tf.variable_scope('aeu'):
self.lfc=self.encoder(self.X)
self.Y_pred, self.Y_pred_modi = self.decoder(self.lfc)
enocder和解码器如下所示
def encoder(self,X):
with tf.device('/gpu:'+GPU0):
X = tf.reshape(X,[-1, vox_res64,vox_res64,vox_res64,1])
c_e = [1,64,128,256,512]
s_e = [0,1 , 1, 1, 1]
layers_e = []
layers_e.append(X)
for i in range(1,5,1):
layer = tools.Ops.conv3d(layers_e[-1],k=4,out_c=c_e[i],str=s_e[i],name='e'+str(i))
layer = tools.Ops.maxpool3d(tools.Ops.xxlu(layer, label='lrelu'), k=2,s=2,pad='SAME')
layers_e.append(layer)
### fc
[_, d1, d2, d3, cc] = layers_e[-1].get_shape()
d1=int(d1); d2=int(d2); d3=int(d3); cc=int(cc)
lfc = tf.reshape(layers_e[-1],[-1, int(d1)*int(d2)*int(d3)*int(cc)])
lfc = tools.Ops.xxlu(tools.Ops.fc(lfc, out_d=500,name='fc1'), label='relu')
print (d1)
print(cc)
return lfc
def decoder(self,Z):
with tf.device('/gpu:'+GPU0):
lfc = tools.Ops.xxlu(tools.Ops.fc(Z, out_d=2*2*2*512, name='fc2'), label='relu')
lfc = tf.reshape(lfc, [-1,2,2,2,512])
c_d = [0,256,128,64]
s_d = [0,2,2,2]
layers_d = []
layers_d.append(lfc)
for j in range(1,4,1):
layer = tools.Ops.deconv3d(layers_d[-1],k=4,out_c=c_d[j],str=s_d[j],name='d'+str(len(layers_d)))
layer = tools.Ops.xxlu(layer, label='relu')
layers_d.append(layer)
###
layer = tools.Ops.deconv3d(layers_d[-1],k=4,out_c=1,str=2,name='dlast')
print("****************************",layer)
###
Y_sig = tf.nn.sigmoid(layer)
Y_sig_modi = tf.maximum(Y_sig,0.01)
return Y_sig, Y_sig_modi
当我在训练后尝试使用模型时
X = tf.get_default_graph().get_tensor_by_name("Placeholder:0")
Z = tf.get_default_graph().get_tensor_by_name("Placeholder_1:0")
Y_pred = tf.get_default_graph().get_tensor_by_name("aeu/Sigmoid:0")
lfc = tf.get_default_graph().get_tensor_by_name("aeu/Relu:0")
获取潜在代码可以很好地工作
lc = sess.run(lfc, feed_dict={X: x_sample})
现在我想使用潜在代码作为解码器的输入,我得到一个错误,我必须填写X(占位符)
我怎样才能拆分到编码器-解码器?
我只搜索了我找到的keras示例我注意到的第一件事是,您没有在任何地方将self.Z传递到解码器中。所以tensorflow不能自动将占位符和之前使用的z链接起来 你可以做一些事情来解决这个问题。最简单的方法是尝试重新创建解码器图,但在调用变量范围时,请将reuse设置为True
使用tf.variable_scope('aeu',reuse=True):
self.new_Y,self.new_Y_modi=self.decoder(self.Z)
y_pred=sess.run(self.new_y,feed_dict={self.Z:lc})
这可能是最容易做到的方法。在这种情况下,可能会要求您填写占位符X,但您可以使用空数组来填写占位符X。通常Tensorflow不会要求它,除非有某种控制依赖将两者联系在一起 我找到了如何分割模型 如果有人想知道,我会给出答案 我的错误是: 1:我没有将self.Z传递给解码器 2:对于以下行
y_pred = sess.run(Y_pred, feed_dict={Z: lc})
在我训练了我的模型之后,这一行在不同的文件中
tensorflow不知道[z]指的是什么,因此您必须使用与确定张量相同的变量,如下所示
lfc = tf.get_default_graph().get_tensor_by_name("aeu/Relu:0")
我把它命名为[利物浦]而不是[Z]
因此,更改代码以解决问题
y_pred = sess.run(Y_pred, feed_dict={lfc: lc})
y_pred = sess.run(Y_pred, feed_dict={lfc: lc})