【问题标题】:change output layer of tensorflow during learning在学习期间改变张量流的输出层
【发布时间】:2018-04-26 12:00:31
【问题描述】:

我有一个使用 tensorflow 的自动编码器。我的代码附在这里:

import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', validation_size=10000)
w=np.random.randint(2,size=60000)
img = mnist.train.images[2]
plt.imshow(img.reshape((28, 28)), cmap='Greys_r')

learning_rate = 0.001
# Input and target placeholders
inputs_ = tf.placeholder(tf.float32, (None, 28,28,1), name="input")
targets_ = tf.placeholder(tf.float32, (None, 28,28,1), name="target")

### Encoder
conv1 = tf.layers.conv2d(inputs=inputs_, filters=16, kernel_size=(3,3), padding='same', activation=tf.nn.relu)
# Now 28x28x16
maxpool1 = tf.layers.max_pooling2d(conv1, pool_size=(2,2), strides=(2,2), padding='same')
# Now 14x14x16
conv2 = tf.layers.conv2d(inputs=maxpool1, filters=8, kernel_size=(3,3), padding='same', activation=tf.nn.relu)
# Now 14x14x8
maxpool2 = tf.layers.max_pooling2d(conv2, pool_size=(2,2), strides=(2,2), padding='same')
# Now 7x7x8
conv3 = tf.layers.conv2d(inputs=maxpool2, filters=8, kernel_size=(3,3), padding='same', activation=tf.nn.relu)
# Now 7x7x8
encoded = tf.layers.max_pooling2d(conv3, pool_size=(2,2), strides=(2,2), padding='same')
# Now 4x4x8


### Decoder
upsample1 = tf.image.resize_images(encoded, size=(7,7), method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
# Now 7x7x8
conv4 = tf.layers.conv2d(inputs=upsample1, filters=8, kernel_size=(3,3), padding='same', activation=tf.nn.relu)
# Now 7x7x8
upsample2 = tf.image.resize_images(conv4, size=(14,14), method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
# Now 14x14x8
conv5 = tf.layers.conv2d(inputs=upsample2, filters=8, kernel_size=(3,3), padding='same', activation=tf.nn.relu)
# Now 14x14x8
upsample3 = tf.image.resize_images(conv5, size=(28,28), method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
# Now 28x28x8
conv6 = tf.layers.conv2d(inputs=upsample3, filters=16, kernel_size=(3,3), padding='same', activation=tf.nn.relu)
# Now 28x28x16

logits = tf.layers.conv2d(inputs=conv6, filters=1, kernel_size=(3,3), padding='same', activation=None)
#Now 28x28x1

# Pass logits through sigmoid to get reconstructed image
decoded = tf.nn.sigmoid(logits)

# Pass logits through sigmoid and calculate the cross-entropy loss
loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=targets_, logits=logits)

# Get cost and define the optimizer
cost = tf.reduce_mean(loss)
opt = tf.train.AdamOptimizer(learning_rate).minimize(cost)
sess = tf.Session()
epochs = 1
batch_size = 200
sess.run(tf.global_variables_initializer())
for e in range(epochs):
    for ii in range(mnist.train.num_examples//batch_size):
        batch = mnist.train.next_batch(batch_size)
        imgs = batch[0].reshape((-1, 28, 28, 1))
        batch_cost, _ = sess.run([cost, opt], feed_dict={inputs_: imgs,
                                                         targets_: imgs})
        Fprim=tf.reshape(encoded,[-1,1])
        temp=Fprim[0]
        Fprim[0]=Fprim[1]
        Fprim[1]=temp
        encoded=tf.reshape(Fprim,[4,4,8])



        print("Epoch: {}/{}...".format(e+1, epochs),
              "Training loss: {:.4f}".format(batch_cost))

fig, axes = plt.subplots(nrows=2, ncols=10, sharex=True, sharey=True, figsize=(20,4))
in_imgs = mnist.test.images[:10]
noisy_imgs = in_imgs + 0.01 * np.random.randn(*in_imgs.shape)
noisy_imgs = np.clip(noisy_imgs, 0., 1.)

reconstructed = sess.run(decoded, feed_dict={inputs_: noisy_imgs.reshape((10, 28, 28, 1))})

for images, row in zip([noisy_imgs, reconstructed], axes):
    for img, ax in zip(images, row):
        ax.imshow(img.reshape((28, 28)), cmap='Greys_r')
        ax.get_xaxis().set_visible(False)
        ax.get_yaxis().set_visible(False)

fig.tight_layout(pad=0.1)

编码器部分的最终输出命名为编码,我想改变它的两个值并将其发送到解码器部分,所有这些都应该在学习期间完成。但我不知道我能做到吗?如何?在学习过程中如何改变每一层的输出?请指导我。 谢谢。

【问题讨论】:

    标签: tensorflow layer


    【解决方案1】:

    当您使用sess.run() 并在feed_dict=... 中提供提要字典时,您可以覆盖任何张量,而不仅仅是占位符。示例代码(已测试):

    import tensorflow as tf
    
    a = tf.constant( [ 1.0 ] )
    b = tf.constant( [ 2.0 ] )
    c = tf.add( a, b )
    
    with tf.Session() as sess:
        print( sess.run( c ) )
        print( sess.run( c, feed_dict = { b : [ 5.0 ] } ) )
    

    输出:

    [3.]
    [6.]

    所以在你的情况下,当你这样做时

    reconstructed = sess.run(decoded, feed_dict={inputs_: noisy_imgs.reshape((10, 28, 28, 1))})
    

    您可以像这样在提要字典中包含“编码”的更改值:

    reconstructed = sess.run(decoded, feed_dict={
        inputs_: noisy_imgs.reshape((10, 28, 28, 1)),
        encoded : ...the numpy array with the new values... })
    

    【讨论】:

    • 谢谢,但我是初学者,我不明白我应该怎么做:( 在训练部分我有这个 batch_cost, _ = sess.run([cost, opt], feed_dict={ inputs_: imgs,targets_: imgs}) ,现在我想在学习期间更改编码的输出并馈送到网络,如何更改训练部分?我应该使用两个 sess.run ?我何时重塑编码并更改学习过程中的价值?能给我个代码吗?
    猜你喜欢
    • 2018-04-29
    • 1970-01-01
    • 2018-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多