You can build the same model in pytorch. Then extract weights from tensorflow and assign them manually to each layer in pytorch. Depending on the amount of layers it could be time consuming. Building the model depends on the model and I think not everything is possible in pytorch that is possible in tensorflow. Examples how to assign weights in pytorch and extract weights from tensorflow are given below.
Getting weights from tensorflow variables W
and b
:
weights = sess.run(W)
bias = sess.run(b)
where sess
is a tf.Session
.
Assigning weights to pytorch:
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
class Kernel_Emb(nn.Module):
def __init__(self,D_in,H,D_out):
super(Kernel_Emb, self).__init__()
self.linear1 = nn.Linear(D_in,H)
self.linear2 = nn.Linear(H,D_out)
self.linear1.weight = torch.nn.Parameter(torch.zeros(D_in,H)weights)
self.linear1.bias = torch.nn.Parameter(torch.ones(H))
Getting weights from tensorflow variable v
:
sess.run(vbias)
where sess
is a tf.Session
.
Or whenIf the variable is not defined for the datain tensorflow (source: https://stackoverflow.com/questions/36193553/get-the-value-of-some-weights-in-a-model-trained-by-tensorflow):
If you do not currently have a pointer to the
tf.Variable
, you can get a list of the trainable variables in the current graph by calling [tf.trainable_variables()
][4]. This function returns a list of all trainabletf.Variable
objects in the current graph, and you can select the one that you want by matching thev.name
property. For example:# Desired variable is called "tower_2/filter:0". var = [v for v in tf.trainable_variables() if v.name == "tower_2/filter:0"][0]