我尝试对文本数据进行分类,其中df['Addr']是X,df['Reg']是y
Reg
Addr
640022, Ð ÐССÐЯ, ÐУРÐÐÐСÐÐЯ ÐÐÐ, Ð ÐУРÐÐÐ, УРÐÐ... 45
624214, Ð ÐССÐЯ, СÐÐÐ ÐÐÐÐСÐÐЯ ÐÐÐ, Ð ÐÐСÐÐÐ, Ð Ð ... 66
454018, Ð ÐССÐЯ, ЧÐÐЯÐÐÐСÐÐЯ ÐÐÐ, РЧÐÐЯÐÐÐСÐ, У... 74
624022, Ð ÐССÐЯ, СÐÐÐ ÐÐÐÐСÐÐЯ ÐÐÐ, СЫСÐРТСÐÐÐ Ð -... 66
454047, Ð ÐССÐЯ, ЧÐÐЯÐÐÐСÐÐЯ ÐÐÐ, РЧÐÐЯÐÐÐСÐ, У... 74
456787, Ð ÐССÐЯ, ЧÐÐЯÐÐÐСÐÐЯ ÐÐÐ, Ð ÐÐÐРСÐ, УРÐ... 74
450075, Ð ÐССÐЯ, ÐÐШÐÐРТÐСТÐÐ Ð ÐСÐ, РУФÐ, ÐÐ -ÐТ... 3
623854, Ð ÐССÐЯ, СÐÐÐ ÐÐÐÐСÐÐЯ ÐÐÐ, Ð ÐÐ ÐÐТ, УРС... 66
457101, Ð ÐССÐЯ, ЧÐÐЯÐÐÐСÐÐЯ ÐÐÐ, РТРÐÐЦÐ, УРС... 74
640008, Ð ÐССÐЯ, ÐУРÐÐÐСÐÐЯ ÐÐÐ, Ð ÐУРÐÐÐ, ÐÐ -ÐТ... 45
我尝试使用一层tensorflow来分类地址,但它返回
0
取而代之的是相关地区。
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(df['Addr'])
X = csr_matrix(X).todense()
X_train, X_test, y_train, y_test = train_test_split(X, df['Reg'].values.reshape(-1, 1), shuffle=True, test_size=0.2)
# tf
def reset_graph(seed=42):
tf.reset_default_graph()
tf.set_random_seed(seed)
np.random.seed(seed)
def random_batch(X_train, y_train, batch_size):
rnd_indices = np.random.randint(0, X_train.shape[0], batch_size)
X_batch = X_train[rnd_indices]
y_batch = y_train[rnd_indices]
return X_batch, y_batch
reset_graph()
X = tf.placeholder(tf.float32, shape=(None, X_train.shape[1]), name="input")
y = tf.placeholder(tf.float32, shape=(None, y_train.shape[1]), name="y")
y_cls = tf.argmax(y, axis=1)
weights = tf.Variable(tf.truncated_normal([X_train.shape[1], y_train.shape[1]], stddev=0.05), name="weights", trainable=True)
bias = tf.constant(1.0, shape=[y_train.shape[1]], name="bias")
layer_1 = tf.nn.relu_layer(X, weights, bias, name="relu_layer")
outs = tf.nn.softmax(layer_1, name="outs")
y_pred = tf.argmax(outs, axis=1)
cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(logits=layer_1, labels=y)
cost = tf.reduce_mean(cross_entropy)
acc = tf.cast(tf.equal(y_pred, y_cls), tf.float16)
predicted = tf.reduce_sum(acc)
learning_rate = 0.01
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
training_op = optimizer.minimize(cost)
init = tf.global_variables_initializer()
n_epochs = 100
batch_size = 500
n_batches = int(np.ceil(1000 / batch_size))
with tf.Session() as sess:
sess.run(init)
for epoch in range(n_epochs):
for batch_index in range(n_batches):
X_batch, y_batch = random_batch(X_train, y_train, batch_size)
sess.run(training_op, feed_dict={X: X_batch, y: y_batch})
loss_val = cost.eval({X: X_test, y: y_test})
if epoch % 10 == 0:
print("Epoch:", epoch, "\tLoss:", loss_val)
y_proba_val = y_pred.eval(feed_dict={X: X_test, y: y_test})
print(y_test.reshape(1, -1))
print(y_proba_val.reshape(1, -1))
此代码的结果:
Epoch: 0 Loss: 0.0
Epoch: 10 Loss: 0.0
Epoch: 20 Loss: 0.0
Epoch: 30 Loss: 0.0
...
Epoch: 90 Loss: 0.0
[[ 3 66 66 ... 66 66 66]]
[[0 0 0 ... 0 0 0]]
我在程序中找不到错误。
我读过了
softmax
通常用于分类任务,但我对自己的行为没有信心。
0个
?