Tensorflow использует декларативный стиль программирования.Вам нужно объявить , что вы хотите, и только потом вызывать его run
или eval
функции.
1), если вы хотите сделать интерактивную работу с вашей моделью, вам нужно иметь обработчик Session
открытым.Замените первые строки на:
# Launch the graph
sess = tf.Session()
with sess.as_default():
.......
Оригинальный код закрывает сессию, и вы больше не можете использовать обученную модель.Не забудьте вызвать sess.close()
, когда вам это не нужно, чтобы высвободить ресурсы, выделенные для TF.
2) Теперь вам нужно преобразовать текст, который вы хотите классифицировать, в представление числового тензора.В оригинальном коде это делается с помощью get_batch()
.Следуйте той же схеме.
3) Объявите результат.Ваша модель связана с переменной prediction
.
4) Вызов TF.Итоговый код выглядит так:
texts = ['''By '8 grey level images' you mean 8 items of 1bit images?
It does work(!), but it doesn't work if you have more than 1bit
in your screen and if the screen intensity is non-linear.''',
'''Wanted: Shareware graphics display program for DOS.
Distribution: usa\nOrganization: University of Notre Dame, Notre Dame
Lines: 16 I need a graphics display program that can take as a parameter the name of
the file to be displayed, then just display that image and then quit.
All of the other graphics display programs come up with a menu first or some other silliness.
This program is going to be run from within another program. '''
]
# convert texts to tensors
batch = []
for text in texts:
vector = np.zeros(total_words,dtype=float)
for word in text.split(' '):
if word in word2index:
vector[word2index[word.lower()]] += 1
batch.append(vector)
x_in = np.array(batch)
# declare new Graph node variable
category = tf.argmax(prediction,1) # choose by maximum score
# run TF
with sess.as_default():
print("scores:", prediction.eval({input_tensor: x_in}))
print('class:', category.eval({input_tensor: x_in}))
Out[]:
scores: [[-785.557 -781.1719 105.238686]
[ 554.584 -532.36383 263.20908 ]]
class: [2 0]