0

I am trying to train an AI program that predicts stock values. Every single time, my cost is 0 and my test is 100%. I can not seem to find what I am doing wrong.

placeholder1 = tf.placeholder(tf.float32, shape=[None, 3])


#trainers
dates_train = np.array(dates[0:8000]).astype(np.float32)
highPrice_train = np.array(highPrice[0:8000]).astype(np.float32)
print(dates_train[0][0])

#testers
dates_test = np.array(dates[8000:9564]).astype(np.float32)
highPrice_test = np.array(highPrice[8000:9564]).astype(np.float32)

def get_training_batch(n):
    n = min(n,7999)
    idx = np.random.choice(7999,n)
    return dates_train[idx],highPrice_train[idx]

n_hidden_1 = 100
n_hidden_2 = 100

weights = {
    'h1' : tf.Variable(tf.random_normal([3, n_hidden_1])),
    'h2' : tf.Variable(tf.random_normal([n_hidden_1,n_hidden_2])),
    'out' : tf.Variable(tf.random_normal([n_hidden_2,1]))
}

biases = {
    'b1' : tf.Variable(tf.random_normal([n_hidden_1])),
    'b2' : tf.Variable(tf.random_normal([n_hidden_2])),
    'out' : tf.Variable(tf.random_normal([1]))
}

layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(placeholder1, weights['h1']), biases['b1']))
layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1, weights['h2']), biases['b2']))

y = tf.matmul(layer_2,weights['out']) + biases['out']

placeholder2 = tf.placeholder(tf.float32,shape=[None,1])
print("Mean")
print(sum(highPrice)/len(highPrice))

mean = tf.reduce_mean(highPrice)
print(mean)

cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=y, labels=placeholder2))
print("Printing cross_entropy")
print(cross_entropy)

rate = 0.01
optimizer = tf.train.GradientDescentOptimizer(rate).minimize(cross_entropy)
print(optimizer)

prediction = tf.nn.softmax(y)
print(prediction)

##Training
correct_prediction = tf.equal(tf.argmax(prediction, 1), tf.argmax(placeholder2,1))
accuracy = 100 * tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(accuracy)

epochs = 1000
batch_size = 10

sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())

cost = []
accu = []
test_accu = []
for ep in range(epochs):
    x_feed,y_feed = get_training_batch(batch_size)
    y_feed = np.reshape(y_feed,[10,1])
    _,cos,predictions,acc = sess.run([optimizer, cross_entropy, prediction, accuracy], feed_dict={placeholder1:x_feed, placeholder2:y_feed})

    highPrice_test = np.reshape(highPrice_test,[1564,1])
    test_acc = accuracy.eval(feed_dict={placeholder1:dates_test, placeholder2:highPrice_test})

    cost.append(cos)
    accu.append(acc)
    test_accu.append(test_acc)

    if(ep % (epochs // 10) == 0):
        print('[%d]: Cos: %.4f, Acc: %.1f%%, Test Acc: %.1f%%' % (ep,cos,acc,test_acc))

plt.plot(cost)
plt.title('cost')
plt.show()

plt.plot(accu)
plt.title('Train Accuracy')
plt.show()

plt.plot(test_accu)
plt.title('Test Accuracy')
plt.show()

index = 36
p = sess.run(prediction, feed_dict = {placeholder1:dates_train[index:index +1]})[0]

[0]: Cos: 0.0000, Acc: 100.0%, Test Acc: 100.0% [100]: Cos: 0.0000, Acc: 100.0%, Test Acc: 100.0%

That is my output for every single test. I expect there to be a cost and accuracy should not be 100%

Kai Aeberli
  • 1,189
  • 8
  • 21
  • what is the shape of dates and of highPrice? – Kai Aeberli Mar 30 '19 at 17:41
  • The shape of dates_train is [8000,3] and highPrice_train is [8000] when it should be [8000,1] ive reshaped it but now i get an error saying there is something wrong with my get_batch_size. "TypeError(_SLICE_TYPE_ERROR + ", got {!r}".format(idx))" – username10101010101010 Mar 30 '19 at 18:58

1 Answers1

1

It seems the problem is that softmax_cross_entropy_with_logits_v2 needs more than 1 output class: Cost function always returning zero for a binary classification in tensorflow. If I change highPrice to 2 dimensional it works.

As a side note, if I understand your problem correctly, you are trying to predict the exact stock price. A better way may be to just predict whether it is going up or down, so you can create categorical labels say (up, no change, down).

import tensorflow as tf

y_dimensions = 2

placeholder1 = tf.placeholder(tf.float32, shape=[None, 3])

dates = np.array([pd.date_range('2012-10-01', periods=10000, freq='10min'),
                  pd.date_range('2012-10-01', periods=10000, freq='20min'),
                  pd.date_range('2012-10-01', periods=10000,
                                freq='30min')]).T

highPrice = np.random.random((10000, y_dimensions)) * 100

# training set
dates_train = np.array(dates[0:8000]).astype(np.float32)
highPrice_train = np.array(highPrice[0:8000]).astype(np.float32)
print("dates train", dates_train[0])

# testing set
dates_test = np.array(dates[8000:9564]).astype(np.float32)
highPrice_test = np.array(highPrice[8000:9564]).astype(np.float32)

def get_training_batch(n):
    n = min(n, 7999)
    idx = np.random.choice(7999, n)  # create size n sample from range 7999
    #print("len batch:", len(idx))
    return dates_train[idx], highPrice_train[idx]

n_hidden_1 = 100
n_hidden_2 = 100

weights = {
    'h1': tf.Variable(tf.random_normal([3, n_hidden_1])),
    'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
    'out': tf.Variable(tf.random_normal([n_hidden_2, y_dimensions]))
}

biases = {
    'b1': tf.Variable(tf.random_normal([n_hidden_1])),
    'b2': tf.Variable(tf.random_normal([n_hidden_2])),
    'out': tf.Variable(tf.random_normal([1]))
}

layer_1 = tf.nn.sigmoid(
    tf.add(tf.matmul(placeholder1, weights['h1']), biases['b1']))
layer_2 = tf.nn.sigmoid(
    tf.add(tf.matmul(layer_1, weights['h2']), biases['b2']))

y = tf.matmul(layer_2, weights['out']) + biases['out']



placeholder2 = tf.placeholder(tf.float32, shape=[None, y_dimensions])

print("Mean:", sum(highPrice) / len(highPrice))

mean = tf.reduce_mean(highPrice)
print("TF mean:", mean)

# labels are high prices, logits are model output
cross_entropy = tf.reduce_mean(
    tf.nn.softmax_cross_entropy_with_logits_v2(logits=y,
                                               labels=placeholder2))
print("cross_entropy:", cross_entropy)

rate = 0.01
optimizer = tf.train.GradientDescentOptimizer(rate).minimize(cross_entropy)
print("optimizer:", optimizer)

prediction = tf.nn.softmax(y)
print("Prediction:", prediction)


##Training
correct_prediction = tf.equal(tf.argmax(prediction, 1),
                              tf.argmax(placeholder2, 1))
accuracy = 100 * tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print("accuracy:", accuracy)

epochs = 300
batch_size = 10


sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())


cost = []
accu = []
test_accu = []
for ep in range(epochs):

    x_feed, y_feed = get_training_batch(batch_size)
    y_feed = np.reshape(y_feed, [batch_size, y_dimensions])
    _, cos, predictions, acc = sess.run(
        [optimizer, cross_entropy, prediction, accuracy],
        feed_dict={placeholder1: x_feed, placeholder2: y_feed})

    highPrice_test = np.reshape(highPrice_test, [1564, y_dimensions])
    test_acc = accuracy.eval(
        feed_dict={placeholder1: dates_test, placeholder2: highPrice_test})

    # create history
    cost.append(cos)
    accu.append(acc)
    test_accu.append(test_acc)

    # every 10 epochs
    if ep % (epochs // 10) == 0:
        print('[%d]: Cos: %.4f, Acc: %.1f%%, Test Acc: %.1f%%' % (
        ep, cos, acc, test_acc))

plt.plot(cost)
plt.title('cost')
plt.show()

plt.plot(accu)
plt.title('Train Accuracy')
plt.show()

plt.plot(test_accu)
plt.title('Test Accuracy')
plt.show()

index = 78
p = sess.run(prediction,
             feed_dict={placeholder1: dates_train[index:index + 1]})[0]

print("final x input for prediction:", dates_train[index:index + 1])
print("final y prediction:", p)

Output:

[0]: Cos: 232.5091, Acc: 50.0%, Test Acc: 50.4%
[30]: Cos: 1119.8948, Acc: 70.0%, Test Acc: 49.6%
[60]: Cos: 554.2071, Acc: 50.0%, Test Acc: 50.4%
[90]: Cos: 668.4500, Acc: 60.0%, Test Acc: 50.4%
[120]: Cos: 1485.1707, Acc: 20.0%, Test Acc: 50.4%
[150]: Cos: 2667.8867, Acc: 50.0%, Test Acc: 50.4%
[180]: Cos: 806.8883, Acc: 50.0%, Test Acc: 50.4%
[210]: Cos: 105.7802, Acc: 50.0%, Test Acc: 49.6%
[240]: Cos: 2002.2031, Acc: 50.0%, Test Acc: 50.4%
[270]: Cos: 3357.0098, Acc: 20.0%, Test Acc: 50.4%
Kai Aeberli
  • 1,189
  • 8
  • 21