问题
在Tensorflow上使用DNNC分类器,永远不会让我的损失低于60左右,测试精度高于40%左右。之前我遇到了一个问题,我的测试精度几乎没有设置为25%,但在对所有输入进行标准化后,我能够将测试精度提高一点,但不是很多。
数据
您需要了解的数据是我有大约127,000个犯罪率数据记录。 15个功能和一个标签。网络的目的是将它们分类到正确的人口四分位数(基于每个县的人口)所以输出标签只有4个等级(0-3)。
代码
import pandas as pd
import tensorflow as tf
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
csv_path = dir_path + "/testing.csv"
CSV_COLUMN_NAMES = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', 'Quartile']
def load_data():
all = pd.read_csv(csv_path, names=CSV_COLUMN_NAMES, header=0).sample(frac=1)
x = all.drop(['Quartile'], axis=1)
y = all[['Quartile']].copy()
size = x.shape[0]
cutoff = int(0.75*size)
train_x = x.head(cutoff)
train_y = y.head(cutoff)
test_x = x.tail(size-cutoff)
test_y = y.tail(size-cutoff)
return (train_x, train_y), (test_x, test_y)
def train_input_fn(features, labels, batch_size):
dataset = tf.data.Dataset.from_tensor_slices((dict(features), labels))
dataset = dataset.shuffle(1000).repeat().batch(batch_size)
return dataset
def eval_input_fn(features, labels, batch_size):
features=dict(features)
if labels is None:
inputs = features
else:
inputs = (features, labels)
dataset = tf.data.Dataset.from_tensor_slices(inputs)
assert batch_size is not None, "batch_size must not be None"
dataset = dataset.batch(batch_size)
return dataset
def main(argv):
batch_size = 50
(train_x, train_y), (test_x, test_y) = load_data()
my_feature_columns = []
for key in train_x.keys():
my_feature_columns.append(tf.feature_column.numeric_column(key=key))
classifier = tf.estimator.DNNClassifier(
feature_columns=my_feature_columns,
hidden_units=[10, 10],
optimizer=tf.train.GradientDescentOptimizer(0.001),
n_classes=4)
# training
classifier.train(
input_fn=lambda:train_input_fn(train_x, train_y, batch_size), steps=5000)
# testing
eval_result = classifier.evaluate(
input_fn=lambda:eval_input_fn(test_x, test_y, batch_size))
print('\nTest set accuracy: {accuracy:0.3f}\n'.format(**eval_result))
if __name__ == '__main__':
tf.logging.set_verbosity(tf.logging.INFO)
tf.app.run(main)
我尝试了什么
MEAN
而不是默认为{/ li>的SUM
我希望你们能够提出任何可能的原因,为什么我的神经网络似乎停滞不前。谢谢!
答案 0 :(得分:1)
您可以查看以下说明:
2
(64,128,256,...)的幂。