日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python字符串标签转化_如何在TensorFlow中将字符串标签转换为一个热向量?

發(fā)布時間:2024/1/23 python 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python字符串标签转化_如何在TensorFlow中将字符串标签转换为一个热向量? 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

我是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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。