Jul-11-2017, 02:09 PM
(This post was last modified: Jul-11-2017, 02:19 PM by ichabod801.)
my question here
I try to use CSV file as the input of the neural network. But I got the warming as 'could not convert string to float: 'train2.CSV'
' My CSV files contain 15 columns. I have no idea how to convert it to float type. My data is over 10K.
Here is my CSV data looks like
12.71 17.76 91.42 30.78 0 0 6.9 0 978 3576 205 326 4500 800 5300
85.12 25.81 9.82 32.48 0 0 6.89 2.12 1036 4039 213 771 1596 4061 5657
91.24 8.56 23.33 30.61 0 0 6.87 1.93 1190 3823 209 631 1599 4065 5664
23.39 18.12 89.47 0 2.64 22.95 6.86 4.26 952 3600 204 933 3745 806 4551
22.65 20.28 81.53 0 1.71 19.32 6.85 4.23 970 3340 200 572 3745 805 4550
32.92 4.92 86.69 34.47 0 0 6.85 1.92 998 3768 209 639 3600 800 4400
15.93 0 83.33 31.93 5.1 22.06 6.84 0 1020 3780 187 443 3548 803 4351
92.41 27.12 17.83 16.83 0 0 6.83 3.62 952 2478 228 906 1500 4000 5500
0 0 145.89 5.1 2.31 0 6.8 4.91 1384 3624 243 842 4800 0 4800
84.45 25.51 10.08 33.16 0 0 6.8 1.5 1152 3603 217 618 1599 4061 5660
I try to use CSV file as the input of the neural network. But I got the warming as 'could not convert string to float: 'train2.CSV'
' My CSV files contain 15 columns. I have no idea how to convert it to float type. My data is over 10K.
Here is my CSV data looks like
12.71 17.76 91.42 30.78 0 0 6.9 0 978 3576 205 326 4500 800 5300
85.12 25.81 9.82 32.48 0 0 6.89 2.12 1036 4039 213 771 1596 4061 5657
91.24 8.56 23.33 30.61 0 0 6.87 1.93 1190 3823 209 631 1599 4065 5664
23.39 18.12 89.47 0 2.64 22.95 6.86 4.26 952 3600 204 933 3745 806 4551
22.65 20.28 81.53 0 1.71 19.32 6.85 4.23 970 3340 200 572 3745 805 4550
32.92 4.92 86.69 34.47 0 0 6.85 1.92 998 3768 209 639 3600 800 4400
15.93 0 83.33 31.93 5.1 22.06 6.84 0 1020 3780 187 443 3548 803 4351
92.41 27.12 17.83 16.83 0 0 6.83 3.62 952 2478 228 906 1500 4000 5500
0 0 145.89 5.1 2.31 0 6.8 4.91 1384 3624 243 842 4800 0 4800
84.45 25.51 10.08 33.16 0 0 6.8 1.5 1152 3603 217 618 1599 4061 5660
import tensorflow as tf
#from tensorflow.examples.tutorials.mnist import input_data
import numpy as np
train_x,train_y,test_x,test_y = ('train2.CSV','trainY.CSV','test2.CSV.','Text Y.CSV.')
n_nodes_hl1 = 500
n_nodes_hl2 = 500
n_nodes_hl3 = 500
n_classes = 2
batch_size = 100
hm_epochs = 10
x = tf.placeholder('float')
y = tf.placeholder('float')
hidden_1_layer = {'f_fum':n_nodes_hl1,
'weight':tf.Variable(tf.random_normal([len(train_x[0]), n_nodes_hl1])),
'bias':tf.Variable(tf.random_normal([n_nodes_hl1]))}
hidden_2_layer = {'f_fum':n_nodes_hl2,
'weight':tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
'bias':tf.Variable(tf.random_normal([n_nodes_hl2]))}
hidden_3_layer = {'f_fum':n_nodes_hl3,
'weight':tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])),
'bias':tf.Variable(tf.random_normal([n_nodes_hl3]))}
output_layer = {'f_fum':None,
'weight':tf.Variable(tf.random_normal([n_nodes_hl3, n_classes])),
'bias':tf.Variable(tf.random_normal([n_classes])),}
# Nothing changes
def neural_network_model(data):
l1 = tf.add(tf.matmul(data,hidden_1_layer['weight']), hidden_1_layer['bias'])
l1 = tf.nn.relu(l1)
l2 = tf.add(tf.matmul(l1,hidden_2_layer['weight']), hidden_2_layer['bias'])
l2 = tf.nn.relu(l2)
l3 = tf.add(tf.matmul(l2,hidden_3_layer['weight']), hidden_3_layer['bias'])
l3 = tf.nn.relu(l3)
output = tf.matmul(l3,output_layer['weight']) + output_layer['bias']
return output
def train_neural_network(x):
prediction = neural_network_model(x)
cost = tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y))
#tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(prediction,y) )
an easier, more compact way.
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)
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(train_x):
start = i
end = i+batch_size
batch_x = np.array(train_x[start:end])
batch_y = np.array(train_y[start:end])
_, c = sess.run([optimizer, cost], feed_dict={x: batch_x,
y: batch_y})
epoch_loss += c
i+=batch_size
print('Epoch', epoch+1, '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:test_x, y:test_y}))
train_neural_network(x)
