I have recently started working with tensorflow and this is like my second piece of code and I am stuck while designing this neural network. I am not able to increment the batch size and this problem has been persisting for quite a while.
import numpy as np
import pandas as pd
import tensorflow as tf
import math
#importing the data and preprocessing it
dataset = pd.read_csv('C:\\Users\\Geeks_Sid\\Documents\\Deep-Learning-A-Z\Deep Learning A-Z\\Volume 1 - Supervised Deep Learning\\Part 1 - Artificial Neural Networks (ANN)\\Section 4 - Building an ANN\\Artificial_Neural_Networks\\Churn_Modelling.csv')
X = dataset.iloc[:, 3:13].values
y = dataset.iloc[:, 13].values
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X_1 = LabelEncoder()
X[:, 1] = labelencoder_X_1.fit_transform(X[:, 1])
labelencoder_X_2 = LabelEncoder()
X[:, 2] = labelencoder_X_2.fit_transform(X[:, 2])
onehotencoder = OneHotEncoder(categorical_features = [1])
X = onehotencoder.fit_transform(X).toarray()
#creating a train test split
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)
# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
# Creating layers for Neural network
n_nodes_hl1 = 1000
n_nodes_hl2 = 1000
n_nodes_hl3 = 1000
n_classes = 1
batch_size = 50
x = tf.placeholder('float', [None, 11])
y = tf.placeholder('float')
def neural_network_model(data):
hidden_1_layer = {'weights':tf.Variable(tf.random_normal([11, 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])),}
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']
print("I was in neural netowrk m")
return output
def train_neural_network(x):
prediction = neural_network_model(x)
# OLD VERSION:
#cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(prediction,y) )
# NEW:
cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y) )
optimizer = tf.train.AdamOptimizer().minimize(cost)
hm_epochs = 10
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
with tf.Session(config=config) as sess:
# OLD:
for epoch in range(hm_epochs):
epoch_loss = 0
current = 0
for _ in range(80):
currentprev = current
current += 100
epoch_x, epoch_y = tuple(X_train[:,currentprev:current]) ,tuple(y_train[:,currentprev:current])
_, c = sess.run([optimizer, cost], feed_dict={x: epoch_x, y: epoch_y})
epoch_loss += c
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_test}))
#sess.run(tf.initialize_all_variables())
# NEW:
sess.run(tf.global_variables_initializer())
correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
train_neural_network(x)
I am dealing with the error that looks like this.
train_neural_network(x)
I was in neural netowrk m
Traceback (most recent call last):
File "<ipython-input-8-7c7cbdae9b34>", line 1, in <module>
train_neural_network(x)
File "<ipython-input-7-b7e263fe7976>", line 20, in train_neural_network
epoch_x, epoch_y = tuple(X_train[:,currentprev:current]) ,tuple(y_train[:,currentprev:current])
IndexError: too many indices for array
I am trying a replicate a code of tensorflow MNIST dataset classification where they used this following piece of code. I hope you're able to compare this code with mine. If there are any corrections, please do help me
def train_neural_network(x):
prediction = neural_network_model(x)
# OLD VERSION:
#cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(prediction,y) )
# NEW:
cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y) )
optimizer = tf.train.AdamOptimizer().minimize(cost)
hm_epochs = 10
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
with tf.Session(config=config) as sess:
# OLD:
#sess.run(tf.initialize_all_variables())
# NEW:
sess.run(tf.global_variables_initializer())
for epoch in range(hm_epochs):
epoch_loss = 0
for _ in range(int(mnist.train.num_examples/batch_size)):
epoch_x, epoch_y = mnist.train.next_batch(batch_size)
_, c = sess.run([optimizer, cost], feed_dict={x: epoch_x, y: epoch_y})
epoch_loss += c
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:mnist.test.images, y:mnist.test.labels}))
As you can see, my code is quite similar to the one for MNIST but i am not able to return a particular tuple which is at this piece of code.
epoch_x, epoch_y = mnist.train.next_batch(batch_size)
_, c = sess.run([optimizer, cost], feed_dict={x: epoch_x, y: epoch_y})
A thanks in advance. if you feel the question is repetitive, i want to explain that i couldn't find others relevant.
I don't understand very well the reshaping you're performing on your data, nor the original format of it but here y = dataset.iloc[:, 13].values it seems that y is a 1D-array while here tuple(y_train[:,currentprev:current] you are accessing it like a 2D matrix and the error is telling you that you are using too many (2) indices to index a 1D array.
Related
I'm trying to write this tensorflow tutorial and I got the below error:
ValueError: No gradients provided for any variable: ['Variable:0',
'Variable_1:0', 'Variable:0', 'Variable_1:0', 'Variable:0',
'Variable_1:0', 'Variable_6:0', 'Variable_7:0'].
import tensorflow as tf
# from tensorflow.examples.tutorials.mnist import input_data
# mnist = input_data.read_data_sets("/tmp/data/", one_hot = True)
mnist = tf.keras.datasets.mnist
n_nodes_hl1 = 500
n_nodes_hl2 = 500
n_nodes_hl3 = 500
n_classes = 10
batch_size = 100
tf.compat.v1.disable_eager_execution()
x = tf.compat.v1.placeholder('float', [None, 784])
y = tf.compat.v1.placeholder('float')
class NN:
def __init__(self):
self.hidden_1_layer = {}
self.hidden_2_layer = {}
self.hidden_3_layer = {}
self.output_layer = {}
def neural_network_model(self,data):
self.hidden_1_layer = {'weights':tf.Variable(tf.compat.v1.random.normal([784, n_nodes_hl1])),
'biases':tf.Variable(tf.compat.v1.random.normal([n_nodes_hl1]))}
self.hidden_2_layer = {'weights':tf.Variable(tf.compat.v1.random.normal([n_nodes_hl1, n_nodes_hl2])),
'biases':tf.Variable(tf.compat.v1.random.normal([n_nodes_hl2]))}
self.hidden_3_layer = {'weights':tf.Variable(tf.compat.v1.random.normal([n_nodes_hl2, n_nodes_hl3])),
'biases':tf.Variable(tf.compat.v1.random.normal([n_nodes_hl3]))}
self.output_layer = {'weights':tf.Variable(tf.compat.v1.random.normal([n_nodes_hl3, n_classes])),
'biases':tf.Variable(tf.compat.v1.random.normal([n_classes])),}
l1 = tf.add(tf.matmul(data,self.hidden_1_layer['weights']), self.hidden_1_layer['biases'])
l1 = tf.nn.relu(l1)
l2 = tf.add(tf.matmul(l1,self.hidden_2_layer['weights']), self.hidden_2_layer['biases'])
l2 = tf.nn.relu(l2)
l3 = tf.add(tf.matmul(l2,self.hidden_3_layer['weights']), self.hidden_3_layer['biases'])
l3 = tf.nn.relu(l3)
output = tf.matmul(l3,self.output_layer['weights']) + self.output_layer['biases']
return output
def train_neural_network(self,x):
prediction = self.neural_network_model(x)
cost = lambda: tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y) )
optimizer = tf.optimizers.Adam().minimize(cost, var_list=[self.hidden_1_layer['weights'],self.hidden_1_layer['biases'],self.hidden_2_layer['weights'],self.hidden_2_layer['biases'],self.hidden_3_layer['weights'],self.hidden_3_layer['biases'],self.output_layer['weights'],self.output_layer['biases']])
hm_epochs = 10
with tf.compat.v1.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(hm_epochs):
epoch_loss = 0
for _ in range(int(mnist.train.num_examples/batch_size)):
epoch_x, epoch_y = mnist.train.next_batch(batch_size)
_, c = sess.run([optimizer, cost], feed_dict={x: epoch_x, y: epoch_y})
epoch_loss += c
print('Epoch', epoch, 'completed out of',hm_epochs,'loss:',epoch_loss)
correct = tf.math.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
print('Accuracy:',accuracy.eval({x:mnist.test.images, y:mnist.test.labels}))
model = NN()
model.train_neural_network(x)
tensorflow version is 2.1.0..
[edit]: to initilize layers in init
where is the problem?
Oh my... If your new to tensorflow, you should really become familiar with layers, training, testing, making models, and such. From what I can tell, what you have above is a detailed expansion of what goes on under the hood of the following code:
import tensorflow as tf
mnist = tf.keras.datasets.mnist.load_data()
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(500,'relu'),
tf.keras.layers.Dense(500,'relu'),
tf.keras.layers.Dense(500,'relu'),
tf.keras.layers.Dense(10,'softmax'),
])
model.compile('adam','sparse_categorical_crossentropy')
model.fit(*mnist[0],validation_data=mnist[1],epochs=1,batch_size=128)
#Trains in 1 min or less on cpu. Few seconds on gpu.
#Plot some data to see what you trained.
import matplotlib.pyplot as plt
plt.imshow(mnist[0][0][0])
plt.show()
#Does this look like a 5?
#Here are first few predictions for the images.
model.predict(mnist[0][0][0:5]).round().argmax(1)
#Notice the first image was predicted to be a 5 by the trained model.
#Here is what we trained to predict.
mnist[0][1][0:5]
#Do our labels match what our model predicts?
#Accuracy (match the predicted against our labels)
matching = model.predict(mnist[1][0]).round().argmax(1) == mnist[1][1]
print(sum(matching)/len(matching)*100)
I got an accuracy of 93.95%. Play with the layers, change 'relu' to 'sigmoid', change the 500 neurons to 400. Try 'sgd' instead of 'adam'. Do more epochs. Increase the batch_size to 1000. What trains faster? What trains slower? Can you get an accuracy of 95%, 98%, 99.9%? Hope this helps.
I'm trying to create a neural network that takes 13 features as input from multiple csv files one at a time and measure accuracy after each iteration. Here is my code snippet:
import tensorflow as tf
import numpy as np
from tensorflow.contrib.layers import fully_connected
import os
import pandas as pd
n_inputs = 13
n_hidden1 = 30
n_hidden2 = 10
n_outputs = 2
learning_rate = 0.01
n_epochs = 40
batch_size = 1
patient_id = os.listdir('./subset_numerical')
output = pd.read_csv('output.csv')
sepsis_pat = output['output'].tolist()
X = tf.placeholder(tf.float32, shape=[None, n_inputs], name="X")
y = tf.placeholder(tf.int64, shape=[None], name="y")
def data_processor(n):
id = pd.read_csv('./subset_numerical/'+patient_id[n])
id_input = np.array([id['VALUE'].tolist()])
for s in sepsis_pat:
if str(s) == str(patient_id[n].split('.')[0]):
a = 1
try:
if a == 1:
a = 0
return [id_input, np.array([1])]
except:
return [id_input, np.array([0])]
def test_set():
id_combined = []
out = []
for p in range(300, len(patient_id)):
try:
id1 = pd.read_csv('./subset_numerical/' + patient_id[p])
id_input1 = np.array(id1['VALUE'].tolist())
id_combined.append(id_input1)
for s in sepsis_pat:
if str(s) == str(patient_id[p].split('.')[0]):
a = 1
try:
if a == 1:
a = 0
out.append([1, 0])
except:
out.append([0, 1])
except:
pass
return [np.array(id_combined), np.array(out)]
# Declaration of hidden layers and calculation of loss goes here
# Construction phase begins
with tf.name_scope("dnn"):
hidden1 = fully_connected(X, n_hidden1, scope="hidden1")
hidden2 = fully_connected(hidden1, n_hidden2, scope="hidden2")
logits = fully_connected(hidden2, n_outputs, scope="outputs", activation_fn=None) # We will apply softmax here later
# Calculating loss
with tf.name_scope("loss"):
xentropy = tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=logits)
loss = tf.reduce_mean(xentropy, name="loss")
# Training with gradient descent optimizer
with tf.name_scope("train"):
optimizer = tf.train.GradientDescentOptimizer(learning_rate)
training_op = optimizer.minimize(loss)
# Measuring accuracy
with tf.name_scope("eval"):
correct = tf.nn.in_top_k(logits, y, 1)
accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))
accuracy_summary = tf.summary.scalar('accuracy', accuracy)
# Variable initialization and saving model goes here
# Construction is finished. Let's get this to work.
with tf.Session() as sess:
init.run()
for epoch in range(n_epochs):
a = 0
for iteration in range(300 // batch_size):
X_batch, y_batch = data_processor(iteration)
sess.run(training_op, feed_dict={X: X_batch, y: y_batch})
acc_train = accuracy.eval(feed_dict={X: X_batch, y: y_batch})
X_test, y_test = test_set()
acc_test = accuracy.eval(feed_dict={X: X_test, y: y_test})
print(epoch, "Train accuracy:", acc_train, "Test accuracy:", acc_test)
save_path = saver.save(sess, "./my_model_final.ckpt")
But I'm stuck with this error:
logits and labels must be same size: logits_size=[1,2] labels_size=[1,1]
The error seems to occur at this line:
correct = tf.nn.in_top_k(logits, y, 1)
What am I doing wrong?
Based on your error log provided, the problem is in this line of your code:
xentropy = tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=logits)
Ensure that both of them have same shape and dtype.
The shape should be of the format [batch_size, num_classes] and dtype should be of type float16, float32 or float64. Check the documentation of softmax_cross_entropy_with_logits for more details.
Since you've defined n_outputs = 2, the shape of logits is [?, 2] (? means batch size), while the shape of y is just [?]. In order to apply the softmax loss function, the last FC layer should return a flat tensor, which can be compared with y.
Solution: set n_outputs = 1.
I am completely loosing my patience with TF and Python, I cant get this to work,
"ValueError: setting an array element with a sequence." on testx when sess.run is invoked.
I have tried a lot of different things.. It's almost as if TF is broken, could anyone assist?
import tensorflow as tf
import numpy as np
nColsIn = 1
nSequenceLen = 4
nBatches = 8
nColsOut = 1
rnn_size = 228
modelx = tf.placeholder("float",[None,nSequenceLen,1])
modely = tf.placeholder("float",[None,nColsOut])
testx = [tf.convert_to_tensor(np.zeros([nColsIn,nBatches])) for b in range(nSequenceLen)]
testy = np.zeros([nBatches, nColsOut])
layer = {
'weights': tf.Variable(tf.random_normal([rnn_size, nColsOut],dtype=tf.float64),),
'biases': tf.Variable(tf.random_normal([nColsOut],dtype=tf.float64))}
lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(rnn_size, forget_bias=1.0)
outputs, states = tf.nn.static_rnn(lstm_cell,modelx ,dtype=tf.float64)
prediction = tf.matmul(outputs[-1], layer['weights']) + layer['biases']
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction,labels=modely))
optimizer = tf.train.AdamOptimizer().minimize(cost)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(modely, 1));
accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
_, epoch_loss = sess.run([optimizer, cost], feed_dict={modelx: testx, modely: testy})
print('Epoch Loss: ',epoch_loss,' Accuracy: ', accuracy.eval({modelx: testx, modely: testy}))
This is probably what you want. You'll find a few remarks in comments in the code.
import tensorflow as tf
import numpy as np
nColsIn = 1
nSequenceLen = 4
nBatches = 8
nColsOut = 1
rnn_size = 228
# As you use static_rnn it has to be a list of inputs
modelx = [tf.placeholder(tf.float64,[nBatches, nColsIn]) for _ in range(nSequenceLen)]
modely = tf.placeholder(tf.float64,[None,nColsOut])
# testx should be a numpy array and is not part of the graph
testx = [np.zeros([nBatches,nColsIn]) for _ in range(nSequenceLen)]
testy = np.zeros([nBatches, nColsOut])
layer = {
'weights': tf.Variable(tf.random_normal([rnn_size, nColsOut],dtype=tf.float64),),
'biases': tf.Variable(tf.random_normal([nColsOut],dtype=tf.float64))}
lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(rnn_size, forget_bias=1.0)
# Replaced testx by modelx
outputs, states = tf.nn.static_rnn(lstm_cell,modelx, dtype=tf.float64)
# output is of shape (8, 4, 128), you probably want the last element in the sequence direction
prediction = tf.matmul(outputs[-1], layer['weights']) + layer['biases']
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction,labels=modely))
optimizer = tf.train.AdamOptimizer().minimize(cost)
if __name__ == '__main__':
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(modely, 1));
accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
feed_dict = {k: v for k,v in zip(modelx, testx)}
feed_dict[modely] = testy
_, epoch_loss = sess.run([optimizer, cost], feed_dict=feed_dict)
print('Epoch Loss: ',epoch_loss,' Accuracy: ', accuracy.eval(feed_dict))
I'm trying to create a NN to approximate functions (sine, cos, custom...) but I'm struggling with the format, I don't want to use input-label, but rather, input-output. How do I change it?
I'm following this tutorial
import tensorflow as tf
import random
from math import sin
import numpy as np
n_nodes_hl1 = 500
n_nodes_hl2 = 500
n_nodes_hl3 = 500
n_inputs = 1 # CHANGES HERE
n_outputs = 1 #CHANGES HERE
batch_size = 100
x = tf.placeholder('float', [None, n_inputs]) #CHANGES HERE
y = tf.placeholder('float', [None, n_outputs]) #CHANGES HERE
def neural_network_model(data):
hidden_layer_1 = {'weights':tf.Variable(tf.random_normal([n_inputs, n_nodes_hl1])),
'biases': tf.Variable(tf.random_normal([n_nodes_hl1]))} #CHANGES HERE
hidden_layer_2 = {'weights':tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
'biases': tf.Variable(tf.random_normal([n_nodes_hl2]))}
hidden_layer_3 = {'weights':tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])),
'biases': tf.Variable(tf.random_normal([n_nodes_hl2]))}
output_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl3, n_outputs])),
'biases': tf.Variable(tf.random_normal([n_outputs]))} #CHANGES HERE
l1 = tf.add(tf.matmul(data, hidden_layer_1['weights']), hidden_layer_1['biases'])
l1 = tf.nn.relu(l1)
l2 = tf.add(tf.matmul(l1, hidden_layer_2['weights']), hidden_layer_2['biases'])
l2 = tf.nn.relu(l2)
l3 = tf.add(tf.matmul(l2, hidden_layer_3['weights']), hidden_layer_3['biases'])
l3 = tf.nn.relu(l3)
output = tf.add(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(logits=prediction, labels=y))
optmizer = tf.train.AdamOptimizer().minimize(cost)
epochs = 10
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(epochs):
loss = 0
for _ in range(batch_size^2): #CHANGES HERE
batch_x, batch_y = generate_input_output(batch_size) #CHANGES HERE
a, c = sess.run([optmizer, cost], feed_dict={x: batch_x, y:batch_y})
loss += c
print("Epoch:", epoch+1, "out of", epochs, "- Loss:", loss)
correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
test_x, test_y = generate_input_output(batch_size) #CHANGES HERE
print('Accuracy', accuracy.eval({x:test_x, y:test_y}))
def desired_function(x): #CHANGES HERE
return sin(x)
def generate_input_output(batch_size): #CHANGES HERE
batch_x = [random.uniform(-10, 10) for _ in range(batch_size)]
batch_y = [desired_function(x) for x in batch_x]
batch_x = np.reshape(batch_x, (-1, 1))
batch_y = np.reshape(batch_y, (-1, 1))
return batch_x, batch_y
train_neural_network(x)
Your solution seems very verbose to me. I'll post a much simplified solution, could you point out what part of it you don't understand?
import tensorflow as tf
import numpy as np
n_nodes_hl1 = 500
n_nodes_hl2 = 500
n_nodes_hl3 = 500
batch_size = 100
x = tf.placeholder('float', [None, 1])
y = tf.placeholder('float', [None, 1])
x1 = tf.contrib.layers.fully_connected(x, n_nodes_hl1)
x2 = tf.contrib.layers.fully_connected(x1, n_nodes_hl2)
x3 = tf.contrib.layers.fully_connected(x2, n_nodes_hl3)
result = tf.contrib.layers.fully_connected(x3, 1,
activation_fn=None)
loss = tf.nn.l2_loss(result - y)
train_op = tf.train.AdamOptimizer().minimize(loss)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(10000):
xpts = np.random.rand(batch_size) * 10
ypts = np.sin(xpts)
_, loss_result = sess.run([train_op, loss],
feed_dict={x: xpts[:, None],
y: ypts[:, None]})
print('iteration {}, loss={}'.format(i, loss_result))
It seems to me that your solution was intended for classification and you have not fully rewrote it for regression, since there are things like softmax_cross_entropy_with_logits left in there, which you certainly don't want in a regression network.
Have not tried it myself, so there might be more things you need to change to get the model to run, but you will definitely want to change this line:
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y))
to something more like:
cost = tf.reduce_sum(tf.square(prediction - y))
Basically, your cost function is much simpler in this case...you just want to reduce the sum of the squared difference between the network's output and the expected y value.
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.