I am a student studying pytorch.
There was a problem converting from TensorFlow to PyTorch.
input signal is ECG signal.
The input I made is [batch_size=128, time_steps=288, input_size=1].
tensorflow code is
def BiRNN(x, _weights, _biases, _keep_prob, _n_hidden):
x = tf.unstack(x, n_steps, 1)
lstm_cell1 = rnn.BasicLSTMCell(_n_hidden, forget_bias=1.0)
lstm_cell1 = rnn.DropoutWrapper(lstm_cell1, output_keep_prob=_keep_prob)
lstm_cell2 = rnn.BasicLSTMCell(_n_hidden, forget_bias=1.0)
lstm_cell2 = rnn.DropoutWrapper(lstm_cell2, output_keep_prob=_keep_prob)
lstm_cell3 = rnn.BasicLSTMCell(_n_hidden, forget_bias=1.0)
lstm_cell3 = rnn.DropoutWrapper(lstm_cell3, output_keep_prob=_keep_prob)
lstm_cells = rnn.MultiRNNCell([lstm_cell1, lstm_cell2, lstm_cell3])
outputs, _ = rnn.static_rnn(lstm_cells, x, dtype=tf.float32)
output = tf.reduce_mean(outputs, axis=0)
fc_pred = tf.matmul(output, _weights['out'])+ _biases['out']
return fc_pred
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost, global_step=global_step)
input size is [batch_size = 128, sequence = 288, input_size = 1 ],
hidden_size is 250,
num_layers is 3,
output size is 18
pytorch code is
class BiRNN(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim, num_layers, batch_size, device):
super(BiRNN, self).__init__()
self.input_dim = input_dim
self.hidden_dim = hidden_dim
self.output_dim = output_dim
self.num_layers = num_layers
self.batch_size = batch_size
self.device = device
self.lstm = nn.LSTM(batch_first=True, input_size=self.input_dim, hidden_size=hidden_dim,
dropout=1.0, num_layers=self.num_layers)
self.linear_1 = nn.Linear(self.hidden_dim, self.output_dim)
def init_hidden(self):
hidden_state = torch.zeros(self.num_layers, self.batch_size, self.hidden_dim)
cell_state = torch.zeros(self.num_layers, self.batch_size, self.hidden_dim)
return hidden_state.to(self.device), cell_state.to(self.device)
def forward(self, x):
(hidden_state, cell_state) = self.init_hidden()
lstm_out, _ = self.lstm(x, (hidden_state, cell_state))
lstm_out = torch.mean(lstm_out, dim=1)
y_pred = self.linear_1(lstm_out)
return y_pred
loss_function = nn.CrossEntropyLoss() # loss function
optimizer = optim.Adam(model.parameters(), lr=0.001) # optimizer
which I made is working. but, The value seems to be wrong
how to convert tensorflow into pytorch??
Any help would be greatly appreciated.