如何在 pytorch 中使用 tfrecord?
我已经下载了具有视频级特征的“Youtube8M”数据集,但它存储在tfrecord中。我尝试从这些文件中读取一些示例,将其转换为 numpy,然后加载到 pytorch 中。但它失败了。
reader = YT8MAggregatedFeatureReader()
files = tf.gfile.Glob("/Data/youtube8m/train*.tfrecord")
filename_queue = tf.train.string_input_producer(
files, num_epochs=5, shuffle=True)
training_data = [
reader.prepare_reader(filename_queue) for _ in range(1)
]
unused_video_id, model_input_raw, labels_batch, num_frames = tf.train.shuffle_batch_join(
training_data,
batch_size=1024,
capacity=1024 * 5,
min_after_dequeue=1024,
allow_smaller_final_batch=True ,
enqueue_many=True)
with tf.Session() as sess:
label_numpy = labels_batch.eval()
print(type(label_numpy))
Run Code Online (Sandbox Code Playgroud)
但这一步却没有任何结果,只是卡了半天没有任何反应。
如何将 numpy ndarry 转换为火炬张量?
这是我的数据:
array([array([-0.4287 , -1.193 , -2.156 , -0.2264 , -1.978 , -1.101 , -3.395 , 0.2974 ], dtype=float16),
array([-0.3386 , 1.398 , -1.083 , 0.2961 , -0.7354 , -1.326 , -4.33 , 0.6284 ], dtype=float16)],
dtype=object)
Run Code Online (Sandbox Code Playgroud) 我正在使用 进行序列分类任务nn.TransformerEncoder()
。其管道类似于nn.LSTM()
.
我尝试了几种时间特征融合方法:
选择最终输出作为整个序列的表示。
使用仿射变换来融合这些特征。
对序列进行逐帧分类,然后选取最大值作为整个序列的类别。
但是,这 3 种方法的准确率都很糟糕,4 类分类的准确率仅为25% 。当使用带有最后一个隐藏状态的 nn.LSTM 时,我可以轻松达到83% 的准确率。我尝试了很多超参数nn.TransformerEncoder()
,但精度没有任何提高。
我现在不知道如何调整这个模型。你能给我一些实用的建议吗?谢谢。
对于LSTM
:forward()
是:
def forward(self, x_in, x_lengths, apply_softmax=False):
# Embed
x_in = self.embeddings(x_in)
# Feed into RNN
out, h_n = self.LSTM(x_in) #shape of out: T*N*D
# Gather the last relevant hidden state
out = out[-1,:,:] # N*D
# FC layers
z = self.dropout(out)
z = self.fc1(z)
z = self.dropout(z)
y_pred = self.fc2(z)
if …
Run Code Online (Sandbox Code Playgroud) machine-learning transformer-model text-classification deep-learning pytorch