tensorflow,训练后拆分自动编码器

2024-10-02 00:20:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我有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.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)

编码器和解码器如下

    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(占位符)

 y_pred = sess.run(Y_pred, feed_dict={Z: lc})

我怎么能分裂到编码器-解码器? 我只搜了几遍,就找到了凯拉斯的例子


Tags: nameselflayergetlayerstfouttools
2条回答

我注意到的第一件事是您没有将self.Z传入解码器。所以tensorflow不能自动地将占位符与之前使用的z链接起来

你可以做一些事情来解决这个问题。最简单的方法是尝试重新创建解码器图,但在调用变量scope时,请将reuse设置为True


    with 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})

相关问题 更多 >

    热门问题