亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在轉換過程中從 tensorflow 對象中提取 numpy 值

在轉換過程中從 tensorflow 對象中提取 numpy 值

海綿寶寶撒 2023-03-30 10:29:27
我正在嘗試使用 tensorflow 獲取詞嵌入,并且我已經使用我的語料庫創建了相鄰的工作列表。我的詞匯表中唯一單詞的數量為 8000,相鄰單詞列表的數量約為 160 萬單詞列表示例照片由于數據非常大,我試圖將單詞列表分批寫入 TFRecords 文件。def save_tfrecords_wordlist(toprocess_word_lists, path ):        writer = tf.io.TFRecordWriter(path)    for word_list in toprocess_word_lists:        features=tf.train.Features(            feature={        'word_list_X': tf.train.Feature( bytes_list=tf.train.BytesList(value=[word_list[0].encode('utf-8')] )),        'word_list_Y': tf.train.Feature( bytes_list=tf.train.BytesList(value=[word_list[1].encode('utf-8') ]))                }            )        example = tf.train.Example(features = features)        writer.write(example.SerializeToString())    writer.close()定義批次batches = [0,250000,500000,750000,1000000,1250000,1500000,1641790]for i in range(len(batches) - 1 ):    batches_start = batches[i]    batches_end = batches[i + 1]    print( str(batches_start) + " -- " + str(batches_end ))    toprocess_word_lists = word_lists[batches_start:batches_end]    save_tfrecords_wordlist( toprocess_word_lists, path +"/TFRecords/data_" + str(i) +".tfrecords")##############################def _parse_function(example_proto):  features = {"word_list_X": tf.io.FixedLenFeature((), tf.string),          "word_list_Y": tf.io.FixedLenFeature((), tf.string)}  parsed_features = tf.io.parse_single_example(example_proto, features)
查看完整描述

1 回答

?
滄海一幻覺

TA貢獻1824條經驗 獲得超5個贊

似乎您無法從映射函數(1、2)內部調用 .numpy() 函數,盡管我能夠使用來自(doc)的 py_function 進行管理。

在下面的示例中,我已將我解析的數據集映射到一個函數,該函數將我的圖像轉換np.uint8以便使用 matplotlib繪制它們。

records_path = data_directory+'TFRecords'+'/data_0.tfrecord'

# Create a dataset

dataset = tf.data.TFRecordDataset(filenames=records_path)

# Map our dataset to the parsing function?

parsed_dataset = dataset.map(parsing_fn)

converted_dataset = parsed_dataset.map(lambda image,label:

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?tf.py_function(func=converting_function,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? inp=[image,label],

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Tout=[np.uint8,tf.int64]))


# Gets the iterator

iterator = tf.compat.v1.data.make_one_shot_iterator(converted_dataset)?


for i in range(5):

? ? image,label = iterator.get_next()

? ? plt.imshow(image)

? ? plt.show()

? ? print('label: ', label)

輸出:

http://img1.sycdn.imooc.com/6424f4bc0001ce7c05110204.jpg

解析函數:

def parsing_fn(serialized):

? ? # Define a dict with the data-names and types we expect to

? ? # find in the TFRecords file.

? ? features = \

? ? ? ? {

? ? ? ? ? ? 'image': tf.io.FixedLenFeature([], tf.string),

? ? ? ? ? ? 'label': tf.io.FixedLenFeature([], tf.int64)? ? ? ? ? ??

? ? ? ? }


? ? # Parse the serialized data so we get a dict with our data.

? ? parsed_example = tf.io.parse_single_example(serialized=serialized,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?features=features)

? ? # Get the image as raw bytes.

? ? image_raw = parsed_example['image']


? ? # Decode the raw bytes so it becomes a tensor with type.

? ? image = tf.io.decode_jpeg(image_raw)

? ??

? ? # Get the label associated with the image.

? ? label = parsed_example['label']

? ??

? ? # The image and label are now correct TensorFlow types.

? ? return image, label

更新:實際上并沒有簽出,但 tf.shape() 似乎也是一個有前途的選擇。



查看完整回答
反對 回復 2023-03-30
  • 1 回答
  • 0 關注
  • 145 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號