ich folgende bin durch das Tutorial hier: https://pythonprogramming.net/train-test-tensorflow-deep-learning-tutorial/Tensorflow: neuronales Netzwerk unter Verwendung positive oder negative Sätze zu klassifizieren
Ich kann das neuronale Netz bekommen trainiert und aus der Genauigkeit drucken.
Ich weiß jedoch nicht, wie man das neurale Netzwerk verwendet, um eine Vorhersage zu treffen.
Hier ist mein Versuch. Insbesondere das Problem ist, diese Linie - ich glaube, mein Problem ist, dass ich nicht meine Eingabezeichenfolge in das Format erwartet das Modell bekommen:
features = get_features_for_input("This was the best store i've ever seen.")
result = (sess.run(tf.argmax(prediction.eval(feed_dict={x:features}),1)))
Hier ist ein größeres Angebot:
def train_neural_network(x):
prediction = neural_network_model(x)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y))
optimizer = tf.train.AdamOptimizer().minimize(cost)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(hm_epochs):
epoch_loss = 0
i = 0
while i < len(train_x):
start = i
end = i + batch_size
batch_x = np.array(train_x[start:end])
batch_y = np.array(train_y[start:end])
_, c = sess.run([optimizer, cost], feed_dict={x: batch_x, y: batch_y})
epoch_loss += c
i+=batch_size
print('Epoch', epoch, 'completed out of', hm_epochs, 'loss:', epoch_loss)
correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct,'float'))
print('Accuracy', accuracy.eval({x:test_x, y:test_y}))
# pos: [1,0] , argmax: 0
# neg: [0,1] , argmax: 1
features = get_features_for_input("This was the best store i've ever seen.")
result = (sess.run(tf.argmax(prediction.eval(feed_dict={x:features}),1)))
if result[0] == 0:
print('Positive:',input_data)
elif result[0] == 1:
print('Negative:',input_data)
def get_features_for_input(input):
current_words = word_tokenize(input.lower())
current_words = [lemmatizer.lemmatize(i) for i in current_words]
features = np.zeros(len(lexicon))
for word in current_words:
if word.lower() in lexicon:
index_value = lexicon.index(word.lower())
# OR DO +=1, test both
features[index_value] += 1
features = np.array(list(features))
train_neural_network(x)
@Robben_Ford_Fan_boy: Hat es deine Frage nicht beantwortet? –
Vielen Dank für Ihre Antwort - es wurde sehr geschätzt. Diese Lösung hat für mich funktioniert –