0

I am trying to create a tensorflow layer.

At this point, the goal is very simple. In my custom layer, I want to multiply the input by 2. So every time the input passes through the custom layer it should do the following

input = 2 * input // just multiplying the input by 2

I am using the following code.

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

The line inputs_temp = inputs.eval(session=tf.Session()) gives the following error

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"]()]]

I believe the error is related to session somehow. Is there any to access the input in the custom layer to manipulate them ?

2
  • You have a conceptual problem here, why are you using inputs.eval inside your layer? Commented Apr 28, 2018 at 15:51
  • Ok. Can you please give any suggestion ? I am quite new at this Commented Apr 28, 2018 at 19:34

1 Answer 1

1

It seems from your comment that you just started learning Tensorflow. If that's the case, I highly recommend that you look at Tensorflow "Eager mode". Specifically, the "Programmers Guide" and the YouTube videos. Those are provided by Tensorflow team, and they are extremely helpful.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.