python字符串标签转化_如何在TensorFlow中将字符串标签转换为一个热向量?
我是TensorFlow的新手,想讀一個逗號分隔值(csv)文件,它包含兩列,第1列是索引,第2列是標(biāo)簽字符串。我有以下代碼,它逐行讀取csv文件中的行,并且我能夠使用print語句正確地獲取csv文件中的數(shù)據(jù)。但是,我想從字符串標(biāo)簽進(jìn)行一次熱編碼轉(zhuǎn)換,而不是如何在TensorFlow中進(jìn)行轉(zhuǎn)換。最后一個目標(biāo)是使用tf.train.batch()函數(shù),這樣我就可以得到一批熱標(biāo)簽向量來訓(xùn)練神經(jīng)網(wǎng)絡(luò)。
正如您在下面的代碼中看到的,我可以在TensorFlow會話中為每個標(biāo)簽條目手動創(chuàng)建一個熱向量。但是如何使用tf.train.batch()函數(shù)呢?如果我移動線路label_batch = tf.train.batch([col2], batch_size=5)
進(jìn)入TensorFlow會話塊(用label_one_hot替換col2),程序塊什么也不做。我試圖將一個熱向量轉(zhuǎn)換移到TensorFlow會話之外,但未能使其正常工作。正確的方法是什么?請幫忙。label_files = []
label_files.append(LABEL_FILE)
print "label_files: ", label_files
filename_queue = tf.train.string_input_producer(label_files)
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
print "key:", key, ", value:", value
record_defaults = [['default_id'], ['default_label']]
col1, col2 = tf.decode_csv(value, record_defaults=record_defaults)
num_lines = sum(1 for line in open(LABEL_FILE))
label_batch = tf.train.batch([col2], batch_size=5)
with tf.Session() as sess:
coordinator = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coordinator)
for i in range(100):
column1, column2 = sess.run([col1, col2])
index = 0
if column2 == 'airplane':
index = 0
elif column2 == 'automobile':
index = 1
elif column2 == 'bird':
index = 2
elif column2 == 'cat':
index = 3
elif column2 == 'deer':
index = 4
elif column2 == 'dog':
index = 5
elif column2 == 'frog':
index = 6
elif column2 == 'horse':
index = 7
elif column2 == 'ship':
index = 8
elif column2 == 'truck':
index = 9
label_one_hot = tf.one_hot([index], 10) # depth=10 for 10 categories
print "column1:", column1, ", column2:", column2
# print "onehot label:", sess.run([label_one_hot])
print sess.run(label_batch)
coordinator.request_stop()
coordinator.join(threads)
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的python字符串标签转化_如何在TensorFlow中将字符串标签转换为一个热向量?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python中哪些词是敏感字词_pyth
- 下一篇: websocket python爬虫_p