I am running the code below but it's giving me an error about arrays. I have tried to find a solution and somehow understand the problem but I couldn't solve the problem. Here is my code:
import tensorflow as tf
import pandas as pa
import numpy as np
iris = pa.read_csv("iris.csv", names = ['F1', 'F2', 'F3', 'F4', 'class'])
print(iris.head(5))
iris['class'].value_counts()
#mapping data
A1 = np.asarray([1,0,0])
A2 = np.asarray([0,1,0])
A3 = np.asarray([0,0,1])
Irises = {'Iris-setosa' : A1, 'two' : A2, 'Iris-virginica' : A3}
iris['class'] = iris['class'].map(Irises)
#Mjesanje podataka
iris = iris.iloc[np.random.permutation(len(iris))]
print(iris.head(10))
iris = iris.reset_index(drop=True)
print(iris.head(10))
#splitting data into training and testing
x_train = iris.ix[0:100,['F1', 'F2', 'F3', 'F4']]
y_train = iris.ix[0:100,['class']]
x_test = iris.ix[101:, ['F1', 'F2', 'F3', 'F4']]
y_test = iris.ix[101:, ['class']]
print(x_train.tail(5))
print(y_train.tail(5))
print(x_test.tail(5))
print(y_test.tail(5))
n_nodes_hl1 = 150
n_nodes_hl2 = 150
n_classes = 3 # U ovom slucaju tri, 1-> Iris-setosa, Iris-versicolo, Iris-virginica
batch_size = 50 # Da li ima neko optimalno rijesenje koliko uzeti?
x = tf.placeholder('float', shape = [None, 4]) # 4 featrues
y = tf.placeholder('float', shape = [None, n_classes]) # 3 classes
def neural_network_model(data):
hidden_layer_1 = {'weights': tf.Variable(tf.random_normal([4, n_nodes_hl1])),
'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))}
hidden_layer_2 = {'weights': tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
'biases':tf.Variable(tf.random_normal([n_nodes_hl2]))}
output_layer = {'weights': tf.Variable(tf.random_normal([n_nodes_hl2, n_classes])),
'biases': tf.Variable(tf.random_normal([n_classes]))}
l1 = tf.add(tf.matmul(data, hidden_layer_1['weights']), hidden_layer_1['biases']) #(input_data * weights) + biases
l1 = tf.nn.relu(l1) #activation function, im using rectified
l2 = tf.add(tf.matmul(l1, hidden_layer_2['weights']), hidden_layer_2['biases'])
l2 = tf.nn.relu(l2)
output_layer = tf.matmul(l2, output_layer['weights'] + output_layer['biases'])
return output_layer
def train_neural_network(x):
prediction = neural_network_model(x)
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(prediction, y)) #loss
optimizer = tf.train.GradientDescentOptimizer(0.1).minimize(cross_entropy)
#koliko puta ce ici back
hm_epoch = 10
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
for step in range(hm_epoch):
_, c = sess.run([optimizer, cross_entropy], feed_dict={x: x_train, y:[t for t in y_train.as_matrix()]})
print(c)
correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))
#prediction = sess.run(accuracy, feed_dict=(x: x_test, y:[t for t in y_test.as_matrix()]))
#print(prediction)
train_neural_network(x)
And i get this error:
Traceback (most recent call last): File "NeuralNet.py", line 92, in train_neural_network(x) File "NeuralNet.py", line 83, in train_neural_network _, c = sess.run([optimizer, cross_entropy], feed_dict={x: x_train, y:[t for t in y_train.as_matrix()]}) File "/home/jusuf/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 717, in runrun_metadata_ptr) File "/home/jusuf/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 888, in _run np_val = np.asarray(subfeed_val, dtype=subfeed_dtype) File "/home/jusuf/anaconda3/lib/python3.5/site-packages/numpy/core/numeric.py", line 482, in asarray return array(a, dtype, copy=False, order=order) ValueError: setting an array element with a sequence.