权重和偏差在张量流中不更新 [英] Weights and Biases not updating in tensorflow
问题描述
我已经建立了这个神经网络,以判断房屋是好货还是坏货.由于某些原因,代码没有更新权重和偏差.我的损失保持不变.这是我的代码:
I've made this neural net to figure out whether a house is a good buy or a bad buy. For some reasons the code is not updating weights and biases. My loss stays same. This is my code:
我已经建立了这个神经网络,以判断房屋是好货还是坏货.由于某些原因,代码没有更新权重和偏差.我的损失保持不变.这是我的代码:
I've made this neural net to figure out whether a house is a good buy or a bad buy. For some reasons the code is not updating weights and biases. My loss stays same. This is my code:
import pandas as pd
import tensorflow as tf
data = pd.read_csv("E:/workspace_py/datasets/good_bad_buy.csv")
features = data.drop(['index', 'good buy'], axis = 1)
lbls = data.drop(['index', 'area', 'bathrooms', 'price', 'sq_price'], axis = 1)
features = features[0:20]
lbls = lbls[0:20]
print(features)
print(lbls)
n_examples = len(lbls)
# Model
# Hyper parameters
epochs = 100
learning_rate = 0.1
batch_size = 1
input_data = tf.placeholder('float', [None, 4])
labels = tf.placeholder('float', [None, 1])
weights = {
'hl1': tf.Variable(tf.random_normal([4, 10])),
'hl2': tf.Variable(tf.random_normal([10, 10])),
'hl3': tf.Variable(tf.random_normal([10, 4])),
'ol': tf.Variable(tf.random_normal([4, 1]))
}
biases = {
'hl1': tf.Variable(tf.random_normal([10])),
'hl2': tf.Variable(tf.random_normal([10])),
'hl3': tf.Variable(tf.random_normal([4])),
'ol': tf.Variable(tf.random_normal([1]))
}
hl1 = tf.nn.relu(tf.add(tf.matmul(input_data, weights['hl1']), biases['hl1']))
hl2 = tf.nn.relu(tf.add(tf.matmul(hl1, weights['hl2']), biases['hl2']))
hl3 = tf.nn.relu(tf.add(tf.matmul(hl2, weights['hl3']), biases['hl3']))
ol = tf.nn.sigmoid(tf.add(tf.matmul(hl3, weights['ol']), biases['ol']))
loss = tf.reduce_mean((labels - ol)**2)
train = tf.train.AdamOptimizer(learning_rate).minimize(loss)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
iterations = int(n_examples/batch_size)
for epoch_no in range(epochs):
ptr = 0
for iteration_no in range(iterations):
epoch_input = features[ptr:ptr+batch_size]
epoch_label = lbls[ptr: ptr+batch_size]
ptr = ptr + batch_size
_, err = sess.run([train, loss], feed_dict={input_data: features, labels: lbls})
print("Error at epoch ", epoch_no, ": ", err)
print(sess.run(ol, feed_dict={input_data: [[2104, 3, 399900, 190.0665]]}))
这是数据集:
Features:
area bathrooms price sq_price
0 2104 3 399900 190.066540
1 1600 3 329900 206.187500
2 2400 3 369000 153.750000
3 1416 2 232000 163.841808
4 3000 4 539900 179.966667
5 1985 4 299900 151.083123
6 1534 3 314900 205.280313
7 1427 3 198999 139.452698
8 1380 3 212000 153.623188
9 1494 3 242500 162.315930
10 1940 4 239999 123.710825
11 2000 3 347000 173.500000
12 1890 3 329999 174.602645
13 4478 5 699900 156.297454
14 1268 3 259900 204.968454
15 2300 4 449900 195.608696
16 1320 2 299900 227.196970
17 1236 3 199900 161.731392
18 2609 4 499998 191.643542
19 3031 4 599000 197.624546
labels:
good buy
0 1.0
1 0.0
2 1.0
3 0.0
4 1.0
5 0.0
6 0.0
7 1.0
8 0.0
9 0.0
10 1.0
11 1.0
12 1.0
13 1.0
14 0.0
15 1.0
16 0.0
17 1.0
18 1.0
19 1.0
有关如何解决此问题的任何建议?我尝试过tf.reduce_mean以外的tf.reduce_sum.我还尝试了更大的batch_size.
Any suggestions on how to fix this? I've tried tf.reduce_sum other than tf.reduce_mean. I've also tried a larger batch_size.
推荐答案
需要考虑的几件事
- 无法正确评估Minibatch,因为您输入了功能和lbls而不是epoch_input和epoch_label.
- 您不会以任何方式对数据进行预处理,因此它完全超出范围. IE.我下面的代码将功能标准化为stddev和mean.您可能会考虑使用batch_normalization.
- 您在任何时候都不会评估错误.您需要一套训练有素的测试仪.我下面的代码没有提供数据,但是它会根据错误%进行测试,而不仅仅是损失(这是错误的弱代理,因此您不应将其称为错误).
- 您可以初始化对随机法线的偏见.您可能只想从零开始.
- 您可能应该使用tf.layers或其他高级api.
以下代码实现了95%的训练错误.您想要使用未用于训练来评估测试错误的保留数据集进行测试.
The below code achieves a training error of 95%. You'd want to test with a held out data set not used for training to evaluate the testing error.
#!/usr/bin/env python
import sys
import pandas as pd
import numpy as np
import tensorflow as tf
data = pd.read_csv("data.csv")
features = data.drop(['good buy'], axis = 1)
lbls = data.drop([ 'area', 'bathrooms', 'price', 'sq_price'], axis = 1)
features = features[0:20]
lbls = lbls[0:20]
mu = np.mean(features, axis=0)
sigma = (np.std(features, axis=0))
features = (features - mu) / sigma
n_examples = len(lbls)
# Model
# Hyper parameters
epochs = 100
learning_rate = 0.01
batch_size = 5
input_data = tf.placeholder('float', [None, 4])
labels = tf.placeholder('float', [None, 1])
weights = {
'hl1': tf.Variable(tf.random_normal([4, 10])),
'hl2': tf.Variable(tf.random_normal([10, 10])),
'hl3': tf.Variable(tf.random_normal([10, 4])),
'ol': tf.Variable(tf.random_normal([4, 1]))
}
biases = {
'hl1': tf.Variable(tf.zeros([10])),
'hl2': tf.Variable(tf.zeros([10])),
'hl3': tf.Variable(tf.zeros([4])),
'ol': tf.Variable(tf.zeros([1]))
}
hl1 = tf.nn.relu(tf.add(tf.matmul(input_data, weights['hl1']), biases['hl1']))
hl2 = tf.nn.relu(tf.add(tf.matmul(hl1, weights['hl2']), biases['hl2']))
hl3 = tf.nn.relu(tf.add(tf.matmul(hl2, weights['hl3']), biases['hl3']))
ol = tf.nn.sigmoid(tf.add(tf.matmul(hl3, weights['ol']), biases['ol']))
loss = tf.reduce_mean((labels - ol)**2)
train = tf.train.AdamOptimizer(learning_rate).minimize(loss)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
iterations = int(n_examples/batch_size)
def training_accuracy():
foo, = sess.run([ol], feed_dict={input_data: features, labels: lbls})
return (float(np.count_nonzero(np.equal(np.round(foo), lbls))) / float(lbls.shape[0]))
print("Initial training accuracy %f" % training_accuracy())
for epoch_no in range(epochs):
ptr = 0
for iteration_no in range(iterations):
epoch_input = features[ptr:ptr+batch_size]
epoch_label = lbls[ptr: ptr+batch_size]
ptr = (ptr + batch_size)%len(features)
_, err = sess.run([train, loss], feed_dict={input_data: epoch_input, labels: epoch_label})
print("Error at epoch ", epoch_no, ": ", err)
print(" Training accuracy %f" % training_accuracy())
另外,请不要在github上发布这样的使用问题,它们属于StackOverflow.
Also, please do not post usage questions like this on github, they belong here on StackOverflow.
这篇关于权重和偏差在张量流中不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!