Python 在tensorflow中创建自定义图层时出错

Python 在tensorflow中创建自定义图层时出错,python,tensorflow,deep-learning,keras,Python,Tensorflow,Deep Learning,Keras,我正在尝试创建一个tensorflow层 在这一点上,目标非常简单。在我的自定义层中,我想将输入乘以2。 因此,每次输入通过自定义层时,它都应该执行以下操作 input=2*input//将输入乘以2即可 我正在使用以下代码 from __future__ import print_function import tensorflow as tf import numpy as np import matplotlib.pyplot as plt import tflearn from tfl

我正在尝试创建一个tensorflow层

在这一点上,目标非常简单。在我的自定义层中,我想将输入乘以2。 因此,每次输入通过自定义层时,它都应该执行以下操作

input=2*input//将输入乘以2即可

我正在使用以下代码

from __future__ import print_function

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import tflearn
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.normalization import local_response_normalization
from tflearn.layers.estimator import regression


from tensorflow.examples.tutorials.mnist import input_data
# number 1 to 10 data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)



def add_layer(inputs, out_size, activation_function=None):

    in_size = int(inputs.shape[1])
    Weights = tf.Variable(tf.random_normal([in_size, out_size]))
    biases = tf.Variable(tf.zeros([1, out_size]) + 0.1)

    inputs_temp = inputs.eval(session=tf.Session())

    ############# Do changes to input. Like input = 2 * input #################
    inputs = tf.convert_to_tensor(inputs)

    Wx_plus_b = tf.matmul(inputs, Weights) + biases
    sess = tf.InteractiveSession()

    if activation_function is None:
        outputs = Wx_plus_b
    else:
        outputs = activation_function(Wx_plus_b)
    return outputs

def compute_accuracy(v_xs, v_ys):
    global prediction
    y_pre = sess.run(prediction, feed_dict={xs: v_xs})
    correct_prediction = tf.equal(tf.argmax(y_pre,1), tf.argmax(v_ys,1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    result = sess.run(accuracy, feed_dict={xs: v_xs, ys: v_ys})
    return result



# define placeholder for inputs to network
xs = tf.placeholder(tf.float32, [None, 784]) # 28x28
ys = tf.placeholder(tf.float32, [None, 10])

# Network

network = tflearn.flatten(xs)
network = add_layer(network, 256)
network = tflearn.reshape(network, (-1, 16, 16, 1))
network = conv_2d(network, 32, 3, activation='relu', regularizer="L2")
network = tflearn.flatten(network)
prediction = add_layer(network, 10,  activation_function=tf.nn.softmax)

cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction),
                                              reduction_indices=[1]))       
train_step = tf.train.AdamOptimizer(0.3).minimize(cross_entropy)

sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)

for i in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train_step, feed_dict={xs: batch_xs, ys: batch_ys})
    if i % 50 == 0:
        print(compute_accuracy(
            mnist.test.images, mnist.test.labels))
inputs\u temp=inputs.eval(session=tf.session())
给出了以下错误

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder' with dtype float and shape [?,784]
     [[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[?,784], _device="/job:localhost/replica:0/task:0/device:GPU:0"]()]]
     [[Node: Flatten/Reshape/_1 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_7_Flatten/Reshape", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

我相信这个错误与会话有关。在自定义层中是否有访问输入以对其进行操作的方法

从您的评论来看,您刚刚开始学习Tensorflow。如果是这样的话,我强烈建议你看看Tensorflow的“渴望模式”。具体来说,是和。这些都是Tensorflow团队提供的,非常有用。

这里有一个概念问题,为什么要在层中使用inputs.eval?好的。你能给我一些建议吗?这方面我还不太熟悉