【发布时间】:2017-10-23 13:55:34
【问题描述】:
我正在使用 tensorflow 来训练我自己的全连接网络,但是在前几次迭代中出现显着下降后,网络的损失不再改变,并且损失一直徘徊在 4.3 左右。不知道哪里有问题。改变学习率似乎没有帮助。
我在数据集中使用的样本输入(代码中命名为'feat')是一个长度为13294的稀疏向量,其中只有大约5个位置有效,其余的赋值为1。一批train_x看起来喜欢:
[[1 1 1 1 1 1... - 96...1 1 1 1... - 84...1 1 1 1... - 56...1 1 1 1]
[1 1 1 1 1... - 47...1 1 1 1 1... - 52...1 1 1 1 1.......1 1 1 1 1]
...
]
样本的label是单个值,值在0到137之间。一批train_y的样子:
[
28
28
110
34
...
]
我有 26816 个训练样本用于训练。
使用的代码如下所示
"""Neural network applied with tensroflow.
"""
from __future__ import print_function
import tensorflow as tf
import numpy as np
from scipy.sparse import coo_matrix
file_wifi_feat = 'wifi_feat.npy'
file_shop_label = 'shop_label.npy'
num_shops = 137
num_wifis = 13294
num_hidden_1 = 8192
num_hidden_2 = 2048
num_hidden_3 = 512
num_hidden_4 = 128
num_hidden_5 = 64
class BatchReader:
def __init__(self, feat, label):
self.shuffle = True
self.feat = []
self.label = []
self.batch_offset = 0
self._load_data(feat, label)
def _load_data(self, feat, label):
self.feat = np.load(feat)
self.label = np.load(label)
def next_batch(self, batch_size):
start = self.batch_offset
self.batch_offset += batch_size
if self.batch_offset > self.feat.shape[0]:
perm = np.arange(self.feat.shape[0])
np.random.shuffle(perm)
self.feat = self.feat[perm]
self.label = self.label[perm]
start = 0
self.batch_offset = batch_size
end = self.batch_offset
batch_feat = np.array([m.toarray()[0] for m in self.feat[start:end]])
batch_feat[np.where(batch_feat == 0)] = 1
batch_label = self.label[start:end]
return batch_feat, batch_label
def weight_variable(shape):
"""weight_variable generates a weight variable of a given shape."""
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.get_variable(name='weights', initializer=initial)
def bias_variable(shape):
"""bias_variable generates a bias variable of a given shape."""
initial = tf.constant(0.1, shape=shape)
return tf.get_variable(name='bias', initializer=initial)
def main(argv=None):
batch_reader = BatchReader(file_wifi_feat, file_shop_label)
feat_ph = tf.placeholder(tf.float32, [None, num_wifis])
label_ph = tf.placeholder(tf.int32, [None])
with tf.variable_scope('h1'):
weight = weight_variable([num_wifis, num_hidden_1])
bias = bias_variable([num_hidden_1])
L1 = tf.nn.relu(tf.matmul(feat_ph, weight) + bias)
with tf.variable_scope('h2'):
weight = weight_variable([num_hidden_1, num_hidden_2])
bias = bias_variable([num_hidden_2])
L2 = tf.nn.relu(tf.matmul(L1, weight) + bias)
with tf.variable_scope('h3'):
weight = weight_variable([num_hidden_2, num_hidden_3])
bias = bias_variable([num_hidden_3])
L3 = tf.nn.relu(tf.matmul(L2, weight) + bias)
with tf.variable_scope('h4'):
weight = weight_variable([num_hidden_3, num_hidden_4])
bias = bias_variable([num_hidden_4])
L4 = tf.nn.relu(tf.matmul(L3, weight) + bias)
with tf.variable_scope('h5'):
weight = weight_variable([num_hidden_4, num_hidden_5])
bias = bias_variable([num_hidden_5])
L5 = tf.nn.relu(tf.matmul(L4, weight) + bias)
with tf.variable_scope('hypo'):
weight = weight_variable([num_hidden_5, num_shops])
bias = bias_variable([num_shops])
hypothesis = tf.matmul(L5, weight) + bias
loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=hypothesis, labels=label_ph))
correct_prediction = tf.equal(tf.argmax(hypothesis, 1), tf.cast(label_ph, tf.int64))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
global_step = tf.Variable(0, trainable=False)
starter_learning_rate = 0.1
learning_rate = tf.train.exponential_decay(starter_learning_rate, global_step, 20, 0.96, staircase=True)
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss, global_step=global_step)
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
sess.run(tf.initialize_all_variables())
for itr in xrange(100001):
feat, label = batch_reader.next_batch(256)
feed_dict = {feat_ph: feat, label_ph: label}
sess.run(optimizer, feed_dict=feed_dict)
# hypothesis_val = sess.run(hypothesis, feed_dict=feed_dict)
if itr % 10 == 0:
loss_val, accuracy_val, learning_rate_val = sess.run([loss, accuracy, learning_rate], feed_dict=feed_dict)
print('Step %d, loss %g, accuracy %g, learning_rate %g' % (itr, loss_val, accuracy_val, learning_rate_val))
if __name__ == '__main__':
tf.app.run()
输出看起来是这样的(即使运行到第 10000 步,loss 也没有太大变化):
Step 0, loss 1.41158e+09, accuracy 0.03125, learning_rate 0.1
Step 10, loss 4.68047, accuracy 0.0273438, learning_rate 0.1
Step 20, loss 4.54852, accuracy 0.0234375, learning_rate 0.096
Step 30, loss 4.22673, accuracy 0.0546875, learning_rate 0.096
Step 40, loss 4.36984, accuracy 0.0390625, learning_rate 0.09216
Step 50, loss 4.26286, accuracy 0.0546875, learning_rate 0.09216
Step 60, loss 4.4269, accuracy 0.0546875, learning_rate 0.0884736
Step 70, loss 4.21976, accuracy 0.105469, learning_rate 0.0884736
Step 80, loss 4.39736, accuracy 0.0546875, learning_rate 0.0849346
Step 90, loss 4.32979, accuracy 0.0820312, learning_rate 0.0849346
Step 100, loss 4.38875, accuracy 0.078125, learning_rate 0.0815373
Step 110, loss 4.37169, accuracy 0.0898438, learning_rate 0.0815373
...
【问题讨论】:
标签: tensorflow neural-network deep-learning