我有以下计算损失函数的代码:
class MSE_loss(nn.Module):
"""
: metric: L1, L2 norms or cosine similarity
: mode: training or evaluation mode
"""
def __init__(self,metric, mode, weighted_sum = False):
super(MSE_loss, self).__init__()
self.metric = metric.lower()
self.loss_function = nn.MSELoss()
self.mode = mode.lower()
self.weighted_sum = weighted_sum
def forward(self, output1, output2, labels):
self.labels = labels
self.linear = nn.Linear(output1.size()[0],1)
if self.metric == 'cos':
self.d= F.cosine_similarity(output1, output2)
elif self.metric == 'l1':
self.d = torch.abs(output1-output2)
elif self.metric == 'l2':
self.d = torch.sqrt((output1-output2)**2)
def dimensional_reduction(forward):
if self.weighted_sum:
distance = self.linear(self.d)
else:
distance …
Run Code Online (Sandbox Code Playgroud) 我有一个数据框,其中包含两列带有分类变量(更好,相似,更糟)的列。我想提出一个表格,该表格计算这些类别在两列中出现的次数。我正在使用的数据框如下:
Category.x Category.y
1 Better Better
2 Better Better
3 Similar Similar
4 Worse Similar
Run Code Online (Sandbox Code Playgroud)
我想提出一个这样的表:
Category.x Category.y
Better 2 2
Similar 1 2
Worse 1 0
Run Code Online (Sandbox Code Playgroud)
你会怎么做?
我有以下数组:
a = np.random.rand(5,2)
a
array([[0.98736372, 0.07639041],
[0.45342928, 0.4932295 ],
[0.75789786, 0.48546238],
[0.85854235, 0.74868237],
[0.13534155, 0.79317482]])
Run Code Online (Sandbox Code Playgroud)
我想调整它的大小,以便将它分为2个批次,每个批次包含三个元素(根据需要添加零):
array([[[0.98736372, 0.07639041],
[0.45342928, 0.4932295 ],
[0.75789786, 0.48546238]],
[[0.85854235, 0.74868237],
[0.13534155, 0.79317482],
[0, 0]]])
Run Code Online (Sandbox Code Playgroud)
我已经尝试过了,但是它返回None:
a = a.copy()
a.resize((2,3,2), refcheck = False)
Run Code Online (Sandbox Code Playgroud)
我相信.reshape不会提供解决方案,因为它无法填充0以符合所需的数组尺寸。
我有一个大小为[100,70,42]的3D张量(批处理,seq_len,要素),我想通过使用基于线性变换的神经网络来获取大小为[100,1,1]的张量。在Pytorch中为线性)。
我已经实现了以下代码
class Network(nn.Module):
def __init__(self):
super(Network, self).__init__()
self.fc1 = nn.Linear(42, 120)
self.fc2 = nn.Linear(120,1)
def forward(self, input):
model = nn.Sequential(self.fc1,
nn.ReLU(),
self.fc2)
output = model(input)
return output
Run Code Online (Sandbox Code Playgroud)
但是,在训练后,这只会给我输出[100,70,1]的形状,这不是期望的形状。
谢谢!
neural-network dimensionality-reduction deep-learning pytorch tensor
我正在训练一个模型,该模型采用标记化的字符串,然后通过嵌入层和 LSTM。但是,输入中似乎存在错误,因为它没有通过嵌入层。
class DrugModel(nn.Module):
def __init__(self, input_dim, output_dim, hidden_dim, drug_embed_dim,
lstm_layer, lstm_dropout, bi_lstm, linear_dropout, char_vocab_size,
char_embed_dim, char_dropout, dist_fn, learning_rate,
binary, is_mlp, weight_decay, is_graph, g_layer,
g_hidden_dim, g_out_dim, g_dropout):
super(DrugModel, self).__init__()
# Save model configs
self.drug_embed_dim = drug_embed_dim
self.lstm_layer = lstm_layer
self.char_dropout = char_dropout
self.dist_fn = dist_fn
self.binary = binary
self.is_mlp = is_mlp
self.is_graph = is_graph
self.g_layer = g_layer
self.g_dropout = g_dropout
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# For one-hot encoded SMILES
if not is_mlp:
self.char_embed = nn.Embedding(char_vocab_size, char_embed_dim,
padding_idx=0) …
Run Code Online (Sandbox Code Playgroud)