0

I have been struggling with understanding the data passed into tensor flow. I wanted to use tensor flow for classification. I have a dataframe, with 5 features(columns) my Xs and 89 rows (datapoints). I have a target variable 'y' in the 6th column with 5 classes.

entire dataframe is of shape 89 X 6.

Further is the code I have been trying to implement.

import tensorflow as tf    
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X1, y1, test_size=0.3, random_state=45)

#making target variable column in dummy
i = ['tumor'] #6th column name is 'tumor'
y_train1 = pd.get_dummies(y_train, columns = i, drop_first = False)
y_test1 = pd.get_dummies(y_test, columns = i, drop_first = False)
# I am passing target variable as dataframe of dummy variables of my classes. is it correct? Should I split Y variable into dummy variables?

n_nodes_hl1 = 50
n_nodes_hl2 = 50
n_nodes_hl3 = 50
n_classes = 5
batch_size = 10

x = tf.placeholder('float', [None, len(X_train)]) #height X width, part where I am struggling.
y = tf.placeholder('float')


def neural_network_model(data):

    #matching the placeholder's dimension len(X_train) for layer 1
    hidden_1_layer = {'weights':tf.Variable(tf.random_normal([len(X_train), n_nodes_hl1])),
                      'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))}

    hidden_2_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
                      'biases':tf.Variable(tf.random_normal([n_nodes_hl2]))}

    hidden_3_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])),
                      'biases':tf.Variable(tf.random_normal([n_nodes_hl3]))}

    output_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl3, n_classes])),
                      'biases':tf.Variable(tf.random_normal([n_classes]))}

    #input data * weights + biases
    l1 = tf.add(tf.matmul(data,hidden_1_layer['weights']), hidden_1_layer['biases'])
    l1 = tf.nn.relu(l1)

    l2 = tf.add(tf.matmul(l1,hidden_2_layer['weights']), hidden_2_layer['biases'])
    l2 = tf.nn.relu(l2)

    l3 = tf.add(tf.matmul(l2,hidden_3_layer['weights']), hidden_3_layer['biases'])
    l3 = tf.nn.relu(l3)

    output = tf.matmul(l3, output_layer['weights']) + output_layer['biases']

    return output


def train_neural_network(x):
    prediction = neural_network_model(x)
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(prediction, y))

    optimizer = tf.train.AdamOptimizer().minimize(cost) #default learning rate = 0.001

    hm_epochs = 5

    with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())

        for epoch in range(hm_epochs):
            epoch_loss = 0

            i=0
            while i <len(X_train):
                start = i
                end = i + batch_size

                batch_x = np.array(X_train[start:end])
                batch_y = np.array(y_train1[start:end])

                _,c = sess.run([optimizer, cost], feed_dict = {x:batch_x , y:batch_y})
                epoch_loss += c

                i += batch_size

            print ('Epoch', epoch, 'completed out of', hm_epochs, 'loss:', epoch_loss)

        correct = tf.equal(tf.argmax(prediction,1), tf.argmax(y,1))

        accuracy = tf.reduce_mean(tf.cast(correct,'float'))

        print ('accuracy:', accuracy.eval({x:X_test, y:y_test1})) 


train_neural_network(X_train)

With the X_train shape as 62X5, the error is

Argument must be a dense tensor.
[62 rows x 5 columns] - got shape [62, 5], but wanted [].

Can someone please explain about passing data to tensor-flow or placeholder and dimensionality? Thank you.

amen
  • 45
  • 12
  • 'Can someone please explain about passing data to tensor-flow or placeholder and dimensionality? ' Explain what? – Salvador Dali Apr 23 '17 at 20:59
  • Dimensionality to be introduced in the placeholder and shape expected in the first hidden layer. – amen Apr 23 '17 at 21:51

0 Answers0