user2274879
user2274879

Reputation: 349

Implementation of a neural model on Tensor-flow

I am trying to implement a neural network model on Tensor flow but seems to be having problems with the shape of the placeholders. I'm new to TF, hence it could just be a simple misunderstanding. Here's my code and data sample:

_data=[[0.4,0.5,0.6,1],[0.7,0.8,0.9,0],....]

The data comprises of arrays of 4 columns, the last column of each array is the label. I want to classify each array as label 0, label 1 or label 2.

import tensorflow as tf
import numpy as np

_data = datamatrix
X = tf.placeholder(tf.float32, [None, 3])
W = tf.Variable(tf.zeros([3, 1]))
b = tf.Variable(tf.zeros([3]))

init = tf.global_variables_initializer()
Y = tf.nn.softmax(tf.matmul(X, W) + b)

# placeholder for correct labels
Y_ = tf.placeholder(tf.float32, [None, 1])

# loss function
import time
start=time.time()

cross_entropy = -tf.reduce_sum(Y_ * tf.log(Y))

# % of correct answers found in batch
is_correct = tf.equal(tf.argmax(Y,1), tf.argmax(Y_,1))
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))

optimizer = tf.train.GradientDescentOptimizer(0.003)
train_step = optimizer.minimize(cross_entropy)


sess = tf.Session()
sess.run(init)
for i in range(1000):
    # load batch of images and correct answers
    batch_X, batch_Y = [x[:3] for x in _data[:2000]],[x[-1] for x in _data[:2000]]
    train_data={X: batch_X, Y_: batch_Y}

    # train
    sess.run(train_step, feed_dict=train_data)

# success ?
a,c = sess.run([accuracy, cross_entropy], feed_dict=train_data)

I got the following error message after running my code:

ValueError: Cannot feed value of shape (2000,) for Tensor 'Placeholder_1:0', which has shape '(?, 1)'

My desired output should be the performance of the model using cross-entropy; the accuracy value from the codeline below:

a,c = sess.run([accuracy, cross_entropy], feed_dict=train_data)

I would also appreciate any suggestions on how to improve the model, or a model that is more suitable for my data.

Upvotes: 0

Views: 57

Answers (1)

THN
THN

Reputation: 3631

The shape of Placeholder_1:0 Y_, and input data batch_Y is mismatched as specified by the error message. Notice the 1-D vs 2-D array.

So you should either define 1-D place holder:

Y_ = tf.placeholder(tf.float32, [None])

or prepare 2-D data:

    batch_X, batch_Y = [x[:3] for x in _data[:2000]],[x[-1:] for x in _data[:2000]]

Upvotes: 1

Related Questions