Python get_reduce_平均值始终为0
我正在尝试训练一个预测股票价值的人工智能程序。每一次,我的成本是0,我的测试是100%。我似乎找不到我做错了什么Python get_reduce_平均值始终为0,python,tensorflow,artificial-intelligence,stock,Python,Tensorflow,Artificial Intelligence,Stock,我正在尝试训练一个预测股票价值的人工智能程序。每一次,我的成本是0,我的测试是100%。我似乎找不到我做错了什么 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)
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%,测试Acc:100.0%
[100]:Cos:0.0000,Acc:100.0%,测试Acc:100.0%
这是我每次测试的输出。我希望有一个成本和准确性不应该是100%问题似乎是softmax\u cross\u entropy\u与\u logits\u v2需要不止一个输出类:。如果我将highPrice更改为二维,它会工作 顺便说一句,如果我正确理解你的问题,你是在试图预测准确的股价。一个更好的方法可能是预测它是上升还是下降,这样你就可以创建分类标签,比如上升、不变、下降
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)
输出:
[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%
日期和高价的形状是什么?日期列的形状是[8000,3],高价列的形状是[8000],当它应该是[8000,1]时,我对它进行了重塑,但现在我得到一个错误,说我的获取批量大小有问题。TypeError\u SLICE\u TYPE\u ERROR+,获得{!r}.formatIDX非常感谢!很抱歉我还有一个问题。由于我正在读取一个文件,并且我只需要csv中的第二行,我将如何使其成为2D数组?您能否更具体地说:1列始终只是一个一维数组。如果这与上述问题中需要的二维数组y有关,那么这是不可能的。您必须使用不同的损失函数。我试图预测股票的高价,因此我只需要csv文件中的高价以及日期。在这种情况下,我担心您使用了错误的错误函数-softmax用于分类。如果你需要预测一个特定的值,我建议你更多地研究各种各样的误差函数——这更像是回归。