Shreyas
Shreyas

Reputation: 419

How to model data for tensorflow?

I have data of the form :

A   B   C   D   E   F   G
1   0   0   1   0   0   1
1   0   0   1   0   0   1
1   0   0   1   0   1   0
1   0   1   0   1   0   0
...               
1   0   1   0   1   0   0
0   1   1   0   0   0   1
0   1   1   0   0   0   1
0   1   0   1   1   0   0
0   1   0   1   1   0   0

A,B,C,D are my inputs and E,F,G are my outputs. I wrote the following code in Python using TensorFlow:

from __future__ import print_function
#from random import randint

import numpy as np
import tflearn
import pandas as pd


data,labels =tflearn.data_utils.load_csv('dummy_data.csv',target_column=-1,categorical_labels=False, n_classes=None)

 print(data)
 # Build neural network
 net = tflearn.input_data(shape=[None, 4])
 net = tflearn.fully_connected(net, 8)
 net = tflearn.fully_connected(net, 8)
 net = tflearn.fully_connected(net, 3, activation='softmax')
 net = tflearn.regression(net)

 # Define model
 model = tflearn.DNN(net)
 #Start training (apply gradient descent algorithm)
 data_to_array = np.asarray(data)
 print(data_to_array.shape)
 #data_to_array= data_to_array.reshape(6,9)
 print(data_to_array.shape)
 model.fit(data_to_array, labels, n_epoch=10, batch_size=3, show_metric=True)

I am getting an error which says:

ValueError: Cannot feed value of shape (3, 6) for Tensor 'InputData/X:0', which has shape '(?, 4)'

I am guessing this is because my input data has 7 columns (0...6), but I want the input layer to take only the first four columns as input and predict the last 3 columns in the data as output. How can I model this?

Upvotes: 1

Views: 88

Answers (1)

David Parks
David Parks

Reputation: 32061

If the data's in a numpy format, then the first 4 columns are taken with a simple slice:

data[:,0:4]

The : means "all rows", and 0:4 is a range of values 0,1,2,3, the first 4 columns.

If the data isn't in a numpy format, just convert it to a numpy format so you can slice easily.

Here's a related article on numpy slices: Numpy - slicing 2d row or column vector from array

Upvotes: 3

Related Questions