I'm trying to build a Deep Neural Network classifier modeled after the example in the TensorFlow directory. The code of the example is shown here:
def main(unused_argv):
# Load dataset.
iris = learn.datasets.load_dataset('iris')
x_train, x_test, y_train, y_test = cross_validation.train_test_split(
iris.data, iris.target, test_size=0.2, random_state=42)
# Build 3 layer DNN with 10, 20, 10 units respectively.
classifier = learn.DNNClassifier(hidden_units=[10, 20, 10], n_classes=3)
# Fit and predict.
classifier.fit(x_train, y_train, steps=200)
score = metrics.accuracy_score(y_test, classifier.predict(x_test))
print('Accuracy: {0:f}'.format(score))
I'm doing the exact same thing except I'm using my data, which is the same as the iris data (continuous feature values and discrete 0 or 1 target values). A sample of my data is shown here:
G1 G2 G3 G4 Target
7.733347 6.933914 6.493334 5.31336 0
6.555225 6.924448 6.353376 5.568334 1
7.515558 6.326627 6.197123 5.565245 0
7.132243 6.733111 7.107221 5.681575 1
I'm reading my data with the following code:
def extract_examples_labels(filepath):
data = pd.read_csv(filepath).as_matrix()
num_inputs = len(data[0])-1
data_examples = data[:,range(num_inputs)]
data_labels= data[:,len(data[0])-1]
return data_examples, data_labels
I then do the EXACT same thing as in the TensorFlow example but I use my data instead. However, I keep getting an error that says:
ValueError: Target's dtype should be int32, int64 or compatible. Instead got dtype: 'float64'
So I figure this means that since my y_train is a float, I need to cast it to an int so I do so using:
y_train = y_train.astype(int)
I confirm its of type int64 and run the classifier again but get the following error:
ValueError: Targets are incompatible with given information. Given targets: Tensor("output:0", shape=(?,), dtype=int64), required signatures: TensorSignature(dtype=tf.float64, shape=TensorShape([Dimension(None)]), is_sparse=False).
Now it says it wants a float64. So I'm confused what I'm doing wrong. Any suggestions or obvious mistakes?
Target
column is being interpreted asfloat
type. You can do data[['Target']] = data[['Target']].astype(int) to convert them asint
type. – Nickil Maveli