tgs*_*266 4 neural-network python-3.x tensorflow
我正在尝试使用Tensorflow DNN进行Kaggle Competion .数据大约有100列分类数据,29列数值数据和1列输出.我做的是使用Scikit的列车测试分割函数将其分为训练和使用X和y进行测试,其中X是没有"id"的每行的列表或需要预测的值,y是值需要预测.然后我构建了模型,如下所示:
import tensorflow as tf
import numpy as np
import time
import pickle
with open('pickle.pickle', 'rb') as f:
trainX, trainy, testX, testy = pickle.load(f)
trainX = np.array(trainX)
trainy = np.array(trainy)
trainy = trainy.reshape(trainy.shape[0], 1)
testX = np.array(testX)
testy = np.array(testy)
print (trainX.shape)
print (trainy.shape)
testX = testX.reshape(testX.shape[0], 130)
testy = testy.reshape(testy.shape[0], 1)
print (testX.shape)
print (testy.shape)
n_nodes_hl1 = 256
n_nodes_hl2 = 256
n_nodes_hl3 = 256
n_classes = 1
batch_size = 100
# Matrix = h X w
X = tf.placeholder('float', [None, len(trainX[0])])
y = tf.placeholder('float')
def model(data):
hidden_1_layer = {'weights':tf.Variable(tf.random_normal([trainX.shape[1], 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.sigmoid(l1)
l2 = tf.add(tf.matmul(l1, hidden_2_layer['weights']), hidden_2_layer['biases'])
l2 = tf.nn.sigmoid(l2)
l3 = tf.add(tf.matmul(l2, hidden_3_layer['weights']), hidden_3_layer['biases'])
l3 = tf.nn.sigmoid(l3)
output = tf.matmul(l3, output_layer['weights']) + output_layer['biases']
return output
def train(x):
pred = model(x)
#loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(pred, y))
loss = tf.reduce_mean(tf.square(pred - y))
optimizer = tf.train.AdamOptimizer(0.01).minimize(loss)
epochs = 1
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
print ('Beginning Training \n')
for e in range(epochs):
timeS = time.time()
epoch_loss = 0
i = 0
while i < len(trainX):
start = i
end = i + batch_size
batch_x = np.array(trainX[start:end])
batch_y = np.array(trainy[start:end])
_, c = sess.run([optimizer, loss], feed_dict = {x: batch_x, y: batch_y})
epoch_loss += c
i += batch_size
done = time.time() - timeS
print ('Epoch', e + 1, 'completed out of', epochs, 'loss:', epoch_loss, "\nTime:", done, 'seconds\n')
correct = tf.equal(tf.arg_max(pred, 1), tf.arg_max(y, 1))
acc = tf.reduce_mean(tf.cast(correct, 'float'))
print("Accuracy:", acc.eval({x:testX, y:testy}))
train(X)
Run Code Online (Sandbox Code Playgroud)
输出1个时期:
Epoch 1 completed out of 1 loss: 1498498282.5
Time: 1.3765859603881836 seconds
Accuracy: 1.0
Run Code Online (Sandbox Code Playgroud)
我确实意识到损失是非常高的,我使用1个时代只是为了测试目的,是的,我知道我的代码非常混乱.但我想做的就是打印预测.我该怎么办?我知道我需要为X提供一系列功能,但我只是不明白该怎么做.我也不太明白为什么我的准确度为1.0,所以如果你有任何建议,或者任何改变我的代码的方法,我会更乐意倾听任何想法.提前致谢
要获得预测,您只需要评估pred,这是定义模型输出的操作.
怎么做?随着pred.eval().但是您需要输入来预测其预测,因此您必须为feed_dict要eval()处理的样本(或样本)提供字典.
生成的代码如下所示:
predictions = pred.eval(feed_dict = {x:testX})
Run Code Online (Sandbox Code Playgroud)
注意这是非常相似的acc.eval({x:testX, y:testy}),因为这个想法是一样的.你有一个操作(acc在这种情况下),需要一些输入进行评估,你可以通过调用acc.eval()或sess.run(acc)与feed_dict必要的输入相对应来评估它.
| 归档时间: |
|
| 查看次数: |
12381 次 |
| 最近记录: |