使用张量流训练神经网络,为什么这总是预测一个类?

时间:2018-07-28 16:05:32

标签: tensorflow neural-network classification loss-function

这是我的代码,下面是输出: 我使火车样本的两个输出类别的数量相等。但是,模型始终预测一个类别。 [1,0]

我还注意到有时输出为[0,0]-不应将其输出,因为两个类分别为[1,0]和[0,1]。

我对此问题进行了随机森林解答,并且可以得到AUC = 0.9,因此这是一个定义明确且可解决的问题。

# Make results reproducible
seed = 1234
np.random.seed(seed)
tf.set_random_seed(seed)


dataset=datacox_eq

# Loading the dataset
#dataset = pd.read_csv('Iris_Dataset.csv')
dataset = pd.get_dummies(dataset, columns=['is_sellout']) # One Hot Encoding

values = list(dataset.columns.values)

y = dataset[values[-2:]]
y = np.array(y, dtype='float32')
X = dataset[values[1:-2]]
X = np.array(X, dtype='float32')

# Shuffle Data
indices = np.random.choice(len(X), len(X), replace=False)
X_values = X[indices]
y_values = y[indices]

# Creating a Train and a Test Dataset
test_size = 100
X_test = X_values[-test_size:]
X_train = X_values[:-test_size]
y_test = y_values[-test_size:]
y_train = y_values[:-test_size]

# Session
sess = tf.Session()

# Interval / Epochs
interval = 200
epoch = 3000

# Initialize placeholders
X_data = tf.placeholder(shape=[None, 32], dtype=tf.float32)
y_target = tf.placeholder(shape=[None, 2], dtype=tf.float32)

# Input neurons : 4
# Hidden neurons : 8
# Output neurons : 3
hidden_layer_nodes = 64

# Create variables for Neural Network layers
w1 = tf.Variable(tf.random_normal(shape=[32,hidden_layer_nodes])) # Inputs -> Hidden Layer
b1 = tf.Variable(tf.random_normal(shape=[hidden_layer_nodes]))   # First Bias


wx1 = tf.Variable(tf.random_normal(shape=[hidden_layer_nodes,hidden_layer_nodes]))
bx1 = tf.Variable(tf.random_normal(shape=[hidden_layer_nodes]))   


wx2 = tf.Variable(tf.random_normal(shape=[hidden_layer_nodes,hidden_layer_nodes]))
bx2 = tf.Variable(tf.random_normal(shape=[hidden_layer_nodes])) 

wx3 = tf.Variable(tf.random_normal(shape=[hidden_layer_nodes,hidden_layer_nodes]))
bx3 = tf.Variable(tf.random_normal(shape=[hidden_layer_nodes])) 

wx4 = tf.Variable(tf.random_normal(shape=[hidden_layer_nodes,hidden_layer_nodes]))
bx4 = tf.Variable(tf.random_normal(shape=[hidden_layer_nodes])) 



w2 = tf.Variable(tf.random_normal(shape=[hidden_layer_nodes,1])) # Hidden layer -> Outputs
b2 = tf.Variable(tf.random_normal(shape=[2]))   # Second Bias

# Operations
hidden_output = tf.nn.relu(tf.add(tf.matmul(X_data, w1), b1))

hidden_output_1 = tf.nn.relu(tf.add(tf.matmul(hidden_output, wx1), bx1))
hidden_output_2 = tf.nn.relu(tf.add(tf.matmul(hidden_output_1, wx2), bx2))
#hidden_output_3 = tf.nn.relu(tf.add(tf.matmul(hidden_output_2, wx3), bx3))

#hidden_output_4 = tf.nn.relu(tf.add(tf.matmul(hidden_output_3, wx4), bx4))

final_output = tf.nn.softmax(tf.add(tf.matmul(hidden_output_2, w2), b2))


#final_output = tf.nn.softmax(tf.add(tf.matmul(hidden_output, w2), b2))

# Cost Function
#loss = tf.reduce_mean(1 -tf.reduce_sum(y_target * tf.log(final_output), axis=0))
#loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(final_output, y_target))
#loss= -tf.reduce_sum(y_target * tf.log(final_output))
loss= tf.nn.softmax_cross_entropy_with_logits(labels=y_target, logits=final_output)

# Optimizer
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.00000035).minimize(loss)

# Initialize variables
init = tf.global_variables_initializer()
sess.run(init)

# Training
print('Training the model...')
for i in range(1, (epoch + 1)):
    sess.run(optimizer, feed_dict={X_data: X_train, y_target: y_train})
    if i % interval == 0:
        print('Epoch', i, '|', 'Loss:', sess.run(loss, feed_dict={X_data: X_train, y_target: y_train}))

# Prediction
print()
for i in range(len(X_test)):
    print('Actual:', y_test[i], 'Predicted:', np.rint(sess.run(final_output, feed_dict={X_data: [X_test[i]]})))

Training the model...
Epoch 200 | Loss: [0.7572554 0.6329024 0.7572554 ... 0.7572554 0.6329024 0.7572554]
Epoch 400 | Loss: [0.74910045 0.6401595  0.74910045 ... 0.74910045 0.6401595  0.74910045]
Epoch 600 | Loss: [0.73289    0.6475009  0.73289    ... 0.74097717 0.6475009  0.7409772 ]
Epoch 800 | Loss: [0.73289    0.65492356 0.73289    ... 0.73289    0.65492356 0.73289   ]
Epoch 1000 | Loss: [0.7168417  0.66242474 0.7168417  ... 0.7168417  0.67000103 0.7168417 ]
Epoch 1200 | Loss: [0.7168417  0.67000103 0.7168417  ... 0.7168417  0.67000103 0.7168417 ]
Epoch 1400 | Loss: [0.7168417  0.67000103 0.7168417  ... 0.7168417  0.67000103 0.7168417 ]
Epoch 1600 | Loss: [0.7168417  0.67000103 0.7168417  ... 0.7168417  0.67000103 0.7168417 ]
Epoch 1800 | Loss: [0.7168417  0.67764926 0.7168417  ... 0.7168417  0.67000103 0.7168417 ]
Epoch 2000 | Loss: [0.70098954 0.67764926 0.70098954 ... 0.7088891  0.6853658  0.70098954]
Epoch 2200 | Loss: [0.70098954 0.67764926 0.70098954 ... 0.7088891  0.67764926 0.7088891 ]
Epoch 2400 | Loss: [0.7088891  0.67764926 0.7088891  ... 0.7088891  0.67764926 0.7088891 ]
Epoch 2600 | Loss: [0.70098954 0.6853658  0.70098954 ... 0.70098954 0.6853658  0.70098954]
Epoch 2800 | Loss: [0.70098954 0.6853658  0.70098954 ... 0.70098954 0.6853658  0.70098954]
Epoch 3000 | Loss: [0.70098954 0.6853658  0.70098954 ... 0.70098954 0.6853658  0.70098954]

Actual: [0. 1.] Predicted: [[1. 0.]]
Actual: [0. 1.] Predicted: [[1. 0.]]
Actual: [1. 0.] Predicted: [[1. 0.]]
Actual: [1. 0.] Predicted: [[1. 0.]]
Actual: [1. 0.] Predicted: [[1. 0.]]
Actual: [0. 1.] Predicted: [[1. 0.]]
Actual: [0. 1.] Predicted: [[1. 0.]]
Actual: [1. 0.] Predicted: [[1. 0.]]
Actual: [1. 0.] Predicted: [[1. 0.]]
Actual: [1. 0.] Predicted: [[1. 0.]]
Actual: [0. 1.] Predicted: [[1. 0.]]
Actual: [0. 1.] Predicted: [[1. 0.]]
Actual: [0. 1.] Predicted: [[1. 0.]]
Actual: [1. 0.] Predicted: [[1. 0.]]
Actual: [0. 1.] Predicted: [[1. 0.]]
Actual: [0. 1.] Predicted: [[1. 0.]]
Actual: [1. 0.] Predicted: [[1. 0.]]
Actual: [1. 0.] Predicted: [[1. 0.]]
Actual: [1. 0.] Predicted: [[1. 0.]]
Actual: [0. 1.] Predicted: [[1. 0.]]
Actual: [0. 1.] Predicted: [[1. 0.]]
Actual: [0. 1.] Predicted: [[1. 0.]]

1 个答案:

答案 0 :(得分:1)

最终输出需要为非标度的“ logits”。但是您可以使用softmax函数的输出。

尝试

final_output = tf.add(tf.matmul(hidden_output_2, w2), b2)

相反。但这全都记录在Tensorflow文档中,甚至该函数的命名也建议使用未缩放的logit。