在张量流

时间:2018-04-28 10:59:09

标签: python tensorflow deep-learning keras

我正在尝试创建张量流层。

此时,目标非常简单。在我的自定义图层中,我想将输入乘以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_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"]()]]

我认为错误与会话有关。是否可以访问自定义图层中的输入来操作它们?

1 个答案:

答案 0 :(得分:1)

您的评论似乎刚开始学习Tensorflow。如果是这种情况,我强烈建议您查看Tensorflow" Eager模式"。具体来说,"Programmers Guide"YouTube videos。这些是由Tensorflow团队提供的,他们非常有帮助。