从最初的简单实现,到后面一步步的整合代码块,终于达到了可读、便于调试的程度。代码虽然清晰了,但是问题依然存在。目前主要的问题便是权重学习不到东西,loss总是不下降。
目前的版本loss可以下降,很快下降到0,但是查看生成的y值,与真实值差距很大,故推断loss存在问题。
import numpy as np
import pandas as pd
import tensorflow as tf
#转为onehot编码
def turn_onehot(df):
for key in df.columns:
oneHot = pd.get_dummies(df[key])
for oneHotKey in oneHot.columns: #防止重名
oneHot = oneHot.rename(columns={oneHotKey : key+'_'+str(oneHotKey)})
df = df.drop(key, axis=1)
df = df.join(oneHot)
return df
#获取一批次的数据
def get_batch(x_date, y_date, batch):
global pointer
x_date_batch = x_date[pointer:pointer+batch]
y_date_batch = y_date[pointer:pointer+batch]
pointer = pointer + batch
return x_date_batch, y_date_batch
#生成layer
def add_layer(input_num, output_num, x, layer, active=None):
with tf.name_scope('layer'+layer+'/W'+layer):
W = tf.Variable(tf.random_normal([input_num, output_num]), name='W'+layer)
tf.summary.histogram('layer'+layer+'/W'+layer, W)
with tf.name_scope('layer'+layer+'/b'+layer):
b = tf.Variable(tf.zeros([1, output_num])+0.1, name='b'+layer)
tf.summary.histogram('layer'+layer+'/b'+layer, b)
with tf.name_scope('layer'+layer+'/l'+layer):
l = active(tf.matmul(x, W)+b) #使用sigmoid激活函数,备用函数还有relu
tf.summary.histogram('layer'+layer+'/l'+layer, l)
return l
hiddenDim = 200 #隐藏层神经元数
save_file = './train_model.ckpt'
istrain = True
istensorborad = False
pointer = 0
if istrain:
samples = 400
batch = 5 #每批次的数据输入数量
else:
samples = 550
batch = 1 #每批次的数据输入数量
with tf.name_scope('inputdate-x-y'):
#导入
df = pd.DataFrame(pd.read_csv('GHMX.CSV',header=0))
#产生 y_data 值 (1, n)
y_date = df['number'].values
y_date = y_date.reshape((-1,1))
#产生 x_data 值 (n, 4+12+31+24)
df = df.drop('number', axis=1)
df = turn_onehot(df)
x_data = df.values
###生成神经网络模型
#占位符
with tf.name_scope('inputs'):
x = tf.placeholder("float", shape=[None, 71], name='x_input')
y_ = tf.placeholder("float", shape=[None, 1], name='y_input')
#生成神经网络
l1 = add_layer(71, hiddenDim, x, '1', tf.nn.relu)
l2 = add_layer(hiddenDim, hiddenDim, l1, '2', tf.nn.relu)
#l3 = add_layer(hiddenDim, hiddenDim, l2, '3', tf.nn.relu)
#l4 = add_layer(hiddenDim, hiddenDim, l3, '4', tf.nn.relu)
#l5 = add_layer(hiddenDim, hiddenDim, l4, '5', tf.nn.relu)
#l6 = add_layer(hiddenDim, hiddenDim, l5, '6', tf.nn.relu)
#l7 = add_layer(hiddenDim, hiddenDim, l6, '7', tf.nn.relu)
#l8 = add_layer(hiddenDim, hiddenDim, l7, '8', tf.nn.relu)
#l9 = add_layer(hiddenDim, hiddenDim, l8, '9', tf.nn.relu)
y = add_layer(hiddenDim, 1, l2, '10', tf.nn.relu)
#计算loss
with tf.name_scope('loss'):
#loss = tf.reduce_mean(tf.reduce_sum(tf.square(y - y_), name='square'), name='loss') #损失函数,损失不下降,换用别的函数
#loss = -tf.reduce_sum(y_*tf.log(y)) #损失仍然不下降
loss = -tf.reduce_sum(y_*tf.log(tf.clip_by_value(y,1e-10,1.0)) , name='loss')
tf.summary.scalar('loss', loss)
#梯度下降
with tf.name_scope('train_step'):
train_step = tf.train.GradientDescentOptimizer(0.0005).minimize(loss)
#初始化
sess = tf.Session()
if istensorborad:
merged = tf.summary.merge_all()
writer = tf.summary.FileWriter('logs/', sess.graph)
sess.run(tf.initialize_all_variables())
#保存/读取模型
saver = tf.train.Saver()
if not istrain:
saver.restore(sess, save_file)
for i in range(samples):
x_date_batch, y_date_batch = get_batch(x_data, y_date, batch)
feed_dict = {x: x_date_batch, y_: y_date_batch}
if istrain:
sess.run(train_step, feed_dict=feed_dict)
print(y.eval(feed_dict, sess))
else:
sess.run(loss, feed_dict=feed_dict)
print(test_assess.eval(feed_dict, sess))
if istensorborad:
result = sess.run(merged, feed_dict=feed_dict)
writer.add_summary(result,i)
#保存模型
if istrain:
saver.save(sess, save_file)