I'm trying to learn neural networks and found a nice dataset on kaggle where I could predict animal types. When I try to convert code I found about predicting stock prices I get stuck on the shapes of my tensor objects. I understand that for multiplication they need to be [a,b] x [b,c] but can not get them in this format (mainly because part of my objects get the shape (?,16)). Do you have any tips on how to properly understand the shapes of my weights, bias and input? The errors mainly form in the following two lines:
output,W_O = neural_net_model(xs, 1)
layer_2 = tf.add(tf.matmul(layer_1,W_2), b_2)
Sidney
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
import tensorflow as tf
df= pd.read_csv("/Users/larkinfolab01/Documents/TestData/zoo.csv")
df = df.drop(['animal_name'], axis=1)
df_train = df[:70]
df_test = df[70:]
scaler = MinMaxScaler()
X_train = scaler.fit_transform(df_train.drop(['class_type'],axis=1).values)
y_train = scaler.fit_transform(df_train.drop(['class_type'],axis=1).values.reshape(-1, 1))
#y_train = df_train['Close'].as_matrix()
X_test = scaler.fit_transform(df_test.drop(['class_type'],axis=1).values)
y_test = scaler.fit_transform(df_test.drop(['class_type'],axis=1).values.reshape(-1, 1))
#y_test = df_test['Close'].as_matrix()
print("x_train: ",X_train.shape," x_test", X_test.shape, " y_train:", y_train.shape, " y_test", y_test.shape)
print(np.max(y_test),np.max(y_train),np.min(y_test),np.min(y_train))
def denormalize(df,norm_data):
df = df['class_type'].values.reshape(-1,1)
norm_data = norm_data.reshape(-1,1)
scl = MinMaxScaler()
a = scl.fit_transform(df)
new = scl.inverse_transform(norm_data)
return new
def neural_net_model(X_data,input_dim):
print(X_data)
W_1 = tf.Variable(tf.random_uniform(([input_dim,16])))
b_1 = tf.Variable(tf.zeros(16,dtype=tf.float32))
layer_1 = tf.add(tf.matmul(X_data,W_1), b_1)
layer_1 = tf.nn.tanh(layer_1)
W_2 = tf.Variable(tf.random_uniform([-1*(input_dim),16]))
b_2 = tf.Variable(tf.zeros(11,dtype=tf.float32))
layer_2 = tf.add(tf.matmul(layer_1,W_2), b_2)
layer_2 = tf.nn.tanh(layer_2)
W_O = tf.Variable(tf.random_uniform([16,-1]))
b_O = tf.Variable(tf.zeros([1]))
output = tf.add(tf.matmul(layer_1,W_O), b_O)
return output,W_O
xs = tf.placeholder("float")
ys = tf.placeholder("float")
output,W_O = neural_net_model(xs, 1)
cost = tf.reduce_mean(tf.square(output-ys))
train = tf.train.AdamOptimizer(0.001).minimize(cost)
#correct_pred = tf.argmax(output, 1)
#accuracy = tf.reduce_mean(tf.cast(correct_pred,tf.float32))
c_t = []
c_test = []
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
saver = tf.train.Saver()
y_t = denormalize(df_train,y_train)
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
plt.xlabel('Days')
plt.ylabel('Stock Value INR')
plt.title('Nify Stock Index Prediction')
ax.plot(range(len(y_train)), y_t,label='Original')
plt.ion()
print(y_train)
#saver.restore(sess,'yahoo_dataset.ckpt')
for i in range(100):
#sess.run([cost,train],feed_dict={xs:X_train, ys:y_train})
for j in range(X_train.shape[0]):
sess.run([cost,train],feed_dict={xs:X_train[j,:].reshape(1,16), ys:y_train[j]})
print(X_train.shape,"Help")
print(y_train,"plzhelp")
try:
ax.lines.remove(lines[0])
except Exception:
pass
pred = sess.run(output, feed_dict={xs:X_train})
pred = denormalize(df_train,pred)
lines = ax.plot(range(len(y_train)), pred,'r-',label='Prediction')
plt.legend(loc='best')
plt.pause(0.1)
c_t.append(sess.run(cost, feed_dict={xs:X_train,ys:y_train}))
c_test.append(sess.run(cost, feed_dict={xs:X_test,ys:y_test}))
print('Epoch :',i,'class_type :',c_t[i])
pred = sess.run(output, feed_dict={xs:X_test})
for i in range(y_test.shape[0]):
print('Original :',y_test[i],'Predicted :',pred[i])
#plt.plot(range(50),c_t)
#plt.plot(range(50),c_test)
#plt.show()
print('Cost :',sess.run(cost, feed_dict={xs:X_test,ys:y_test}))
y_test = denormalize(df_test,y_test)
pred = denormalize(df_test,pred)
plt.plot(range(y_test.shape[0]),y_test,label="Original Data")
plt.plot(range(y_test.shape[0]),pred,label="Predicted Data")
plt.legend(loc='best')
"""plt.ylabel('Stock Value')
plt.xlabel('Days')
plt.title('Stock Market Nifty')"""
#plt.show()
Comments
Post a Comment