anirudh
anirudh

Reputation: 1

Weights and bias giving Nan

import pandas as pd  
import numpy as np  
from pandas import DataFrame  
from random import shuffle  
import tensorflow as tf  

Taking data from CSV file (IMDB dataset)

data=pd.read_csv('imdb.csv')  
data.fillna(-1)  
features=data.loc[:,['actor_1_facebook_likes','actor_2_facebook_likes','actor_3_facebook_likes','movie_facebook_likes']].as_matrix()  
labels=data.loc[:,['imdb_score']].as_matrix()  


learning_rate=.01  
training_epochs=2000  
display_steps=50  
n_samples=features.size  

Defining placeholders for features and labels:

inputX = tf.placeholder(tf.float32,[None,4])  
inputY = tf.placeholder(tf.float32,[None,1]) 

Defining weights and bias. Weights and bias are coming out to be NaN.

w = tf.Variable(tf.zeros([4,4]))  
b = tf.Variable(tf.zeros([4]))

y_values = tf.add(tf.matmul(inputX,w),b)  

Applying neural network:

y=tf.nn.softmax(y_values)  
cost=tf.reduce_sum(tf.pow(inputY-y,2))/2*n_samples  
optimizer=tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)  

with tf.Session() as sess:  
          sess.run(tf.global_variables_initializer())  
          for i in range(training_epochs):  
              sess.run(optimizer,feed_dict={inputX:features,inputY:labels})  
              if (i) % display_steps==0:  
                 cc=sess.run(cost,feed_dict={inputX:features,inputY:labels})  
                 print(sess.run(w,feed_dict={inputX:features,inputY:labels}))  

Upvotes: 0

Views: 1758

Answers (2)

Avijit Dasgupta
Avijit Dasgupta

Reputation: 2065

Use add_check_numerics_ops of TensorFlow library to check which operation is giving you the nan values.

https://www.tensorflow.org/api_docs/python/tf/add_check_numerics_ops

Upvotes: 1

nessuno
nessuno

Reputation: 27042

Your learning rate is too big (try starting with 1e-3). Also, your neural network won't learn anything because you're starting from a condition in which your weights can't change: you have initialized your weights to zero, that's wrong.

Change your weights initialization to random values in that way:

w = tf.Variable(tf.truncated_normal([4,4]))

and you'll be able to train your network. (biases initialized to 0 are OK)

Upvotes: 2

Related Questions