第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

在轉(zhuǎn)換過程中從 tensorflow 對象中提取 numpy 值

在轉(zhuǎn)換過程中從 tensorflow 對象中提取 numpy 值

海綿寶寶撒 2023-03-30 10:29:27
我正在嘗試使用 tensorflow 獲取詞嵌入,并且我已經(jīng)使用我的語料庫創(chuàng)建了相鄰的工作列表。我的詞匯表中唯一單詞的數(shù)量為 8000,相鄰單詞列表的數(shù)量約為 160 萬單詞列表示例照片由于數(shù)據(jù)非常大,我試圖將單詞列表分批寫入 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條經(jīng)驗 獲得超5個贊

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

在下面的示例中,我已將我解析的數(shù)據(jù)集映射到一個函數(shù),該函數(shù)將我的圖像轉(zhuǎn)換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

解析函數(shù):

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() 似乎也是一個有前途的選擇。



查看完整回答
反對 回復(fù) 2023-03-30
  • 1 回答
  • 0 關(guān)注
  • 132 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號