我在使用张量流的二进制分类问题上得到了一些奇怪的输出,这里是我的代码后面跟着输出。它从50%开始,完全不确定为什么会这样。感谢您的任何帮助。Tensorflow奇怪的输出
成本开始和停留在0不知道为什么这可能是。
成本dosen't变化,它只输出1的数组我不知道为什么。
import pandas as pd
import numpy as np
from sklearn.svm import LinearSVC
from sklearn import neighbors
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
import tensorflow as tf
def weight_var(shape):
initial = tf.truncated_normal(shape, stddev = 0.15)
return tf.Variable(initial)
def bias_var(shape):
initial = tf.constant(0.0, shape=shape)
return tf.Variable(initial)
print("Reading data...")
train = pd.read_csv("../Data/train.csv")
songs = pd.read_csv("../Data/songs.csv")
song_cols = ['song_id', 'artist_name', 'genre_ids', 'language']
y = train.target
train = train.merge(songs[song_cols], on = 'song_id', how='left')
train = train.fillna(-1)
train = train.drop(['target'], axis = 1)
cols = list(train.columns)
for col in cols:
if train[col].dtype == 'object':
train[col] = train[col].apply(str)
le = LabelEncoder()
train_vals = list(train[col].unique())
le.fit(train_vals)
train[col] = le.transform(train[col])
#model = neighbors.KNeighborsClassifier(15)
#features = train.columns.values
#for feature in features:
# mean, std = train[feature].mean(), train[feature].std()
# train.loc[:,feature] = (train[feature] - mean)/std
print(train.head())
x_train, x_test, y_train, y_test = train_test_split(train, y, test_size=0.33, random_state=42)
x_train = x_train.as_matrix()
y_train = np.transpose([y_train.as_matrix()])
y_test = np.transpose([y_test.as_matrix()])
print("Training the model...")
## NEURAL NET ##
#PARAMS#
training_epocs = 20
training_dropout = 0.9
display_step = 5
n_samples = y_train.shape[0]
batch_size = 2048
learning_rate = 0.5
input_nodes = 8
#Multiplier for adjustment of layers
multiplier = 1.5
hidden_nodes1 = 18
hidden_nodes2 = round(hidden_nodes1 * multiplier)
hidden_nodes3 = round(hidden_nodes2 * multiplier)
#Percent of nodes to keep during dropout
percent_keep = tf.placeholder(tf.float32)
#Input Layer
x = tf.placeholder(tf.float32, [None, input_nodes])
#layer 1
W1 = weight_var([input_nodes, hidden_nodes1])
b1 = bias_var([hidden_nodes1])
y1 = tf.nn.sigmoid(tf.matmul(x, W1) + b1)
#Layer 2
W2 = weight_var([hidden_nodes1, hidden_nodes2])
b2 = bias_var([hidden_nodes2])
y2 = tf.nn.sigmoid(tf.matmul(y1, W2) + b2)
#Layer 3
W3 = weight_var([hidden_nodes2, hidden_nodes3])
b3 = bias_var([hidden_nodes3])
y3 = tf.nn.sigmoid(tf.matmul(y2, W3) + b3)
#y3 = tf.nn.dropout(y3, percent_keep)
#Layer 4
W4 = weight_var([hidden_nodes3, 1])
b4 = bias_var([1])
y4 = tf.nn.sigmoid(tf.matmul(y3,W4) + b4)
#output
out = y4
y_ = tf.placeholder(tf.float32)#, [None, 1])
#Cost Function
#cost = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(out),reduction_indices=[1]))
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits = out, labels = y_)
cost = tf.reduce_mean(cross_entropy)
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
#total_error = tf.reduce_sum(tf.square(tf.subtract(y_, tf.reduce_mean(y_))))
#unexplained_error = tf.reduce_sum(tf.square(tf.subtract(y_, y)))
#correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
predicted = out
correct_pred = tf.equal(tf.round(predicted), y_)
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
with tf.Session() as sess:
writer = tf.summary.FileWriter('./board')
writer.add_graph(sess.graph)
sess.run(tf.global_variables_initializer())
for epoch in range(training_epocs):
for batch in range(int(n_samples/batch_size)):
batch_x = x_train[batch*batch_size : (1+batch) * batch_size]
batch_y = y_train[batch*batch_size : (1+batch) * batch_size]
opt, c, acc,output = sess.run([optimizer, cost, accuracy, out], feed_dict={x: batch_x, y_ : batch_y, percent_keep: training_dropout})
avg_c = c/batch_x.shape[0]
if (epoch) % display_step == 0:
print("Epoch: ", epoch,
"Training Error: ", avg_c,
"Train Loss: ", c,
"Accuracy: ", acc,
"Output: ", output)
writer.close()
print("Done")
test_error = tf.nn.l2_loss(y_, name = "SQE")/x_test.shape[0]
print("Test Error:", test_error.eval({x: x_test.as_matrix(), y:y_test}))
您从'x_train'和'y_train'中选择'batch_x'和'batch_y',然后用'x_train'和'y_train'喂食会话,你确定它是正确的吗? –
这是我编辑后的错误,谢谢!它仍然很奇怪,但这是一个很好的结果。 –
看起来它只是给出输出1 –