Ошибка создания пользовательского слоя в tenorflow - PullRequest
0 голосов
/ 28 апреля 2018

Я пытаюсь создать слой тензорного потока.

На данный момент цель очень проста. В моем пользовательском слое я хочу умножить ввод на 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"]()]]

Я считаю, что ошибка как-то связана с сеансом. Есть ли доступ к input в пользовательском слое, чтобы манипулировать ими?

1 Ответ

0 голосов
/ 22 мая 2018

Из вашего комментария видно, что вы только начали изучать Tensorflow. Если это так, я настоятельно рекомендую вам взглянуть на Tensorflow "Eager mode". В частности, «Руководство по программированию» и видео YouTube . Они предоставляются командой Tensorflow, и они чрезвычайно полезны.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...