def __init__(self, args):
super(LSTM, self).__init__(
# RNN
LSTM=L.LSTM(args.n_in_units, args.n_units),
#W_predict=L.Linear(args.n_units, args.n_units),
W_candidate=L.Linear(args.n_in_units, args.n_units),
)
#self.act1 = F.tanh
self.act1 = F.identity
self.args = args
self.n_in_units = args.n_in_units
self.n_units = args.n_units
self.dropout_ratio = args.d_ratio
self.margin = args.margin
self.initialize_parameters()
python类LSTM的实例源码
def __init__(self, n_dim_obs, n_dim_action, n_hidden_channels,
n_hidden_layers):
self.n_input_channels = n_dim_obs
self.n_hidden_layers = n_hidden_layers
self.n_hidden_channels = n_hidden_channels
self.state_stack = []
super().__init__()
with self.init_scope():
self.fc = MLP(in_size=self.n_input_channels,
out_size=n_hidden_channels,
hidden_sizes=[self.n_hidden_channels] *
self.n_hidden_layers)
self.lstm = L.LSTM(n_hidden_channels, n_hidden_channels)
self.out = L.Linear(n_hidden_channels, n_dim_action)
def __init__(self, n_layers=2, eDim=512, hDim=512, name=""):
layers = [0] * n_layers # ????????
for z in six.moves.range(n_layers):
if z == 0: # ???????? eDim
tDim = eDim
else: # ????????????????????????hDim
tDim = hDim
layers[z] = chaLink.LSTM(tDim, hDim)
# log??????????????????????
layers[z].lateral.W.name = name + "_L%d_la_W" % (z + 1)
layers[z].upward.W.name = name + "_L%d_up_W" % (z + 1)
layers[z].upward.b.name = name + "_L%d_up_b" % (z + 1)
super(NLayerLSTM, self).__init__(*layers)
# ????????????LSTM???
def __init__(self, N_SOURCE_VOCAB, N_TARGET_VOCAB, N_EMBED, N_HIDDEN, train=True):
super(EncDecModel, self).__init__(
# Encoder
enc_embed=L.EmbedID(N_SOURCE_VOCAB, N_EMBED),
enc_lstm_1=L.LSTM(N_EMBED, N_HIDDEN),
enc_lstm_2=L.LSTM(N_HIDDEN, N_HIDDEN),
# Decoder initializer
enc_dec_1_c=L.Linear(N_HIDDEN, N_HIDDEN),
enc_dec_1_h=L.Linear(N_HIDDEN, N_HIDDEN),
enc_dec_2_c=L.Linear(N_HIDDEN, N_HIDDEN),
enc_dec_2_h=L.Linear(N_HIDDEN, N_HIDDEN),
# Decoder
dec_embed=L.EmbedID(N_TARGET_VOCAB, N_EMBED),
dec_lstm_1=L.LSTM(N_EMBED, N_HIDDEN),
dec_lstm_2=L.LSTM(N_HIDDEN, N_HIDDEN),
dec_output=L.Linear(N_HIDDEN, N_TARGET_VOCAB),
)
for param in self.params():
param.data[...] = self.xp.random.uniform(-0.08, 0.08, param.data.shape)
self.train = train
self.src_vocab_size = N_SOURCE_VOCAB
self.trg_vocab_size = N_TARGET_VOCAB
self.embed_size = N_EMBED
self.hidden_size = N_HIDDEN
def __init__(self, width=150, height=112, channel=3, action_size=100, latent_size=100):
feature_width = width
feature_height = height
for i in range(4):
feature_width = (feature_width + 1) // 2
feature_height = (feature_height + 1) // 2
feature_size = feature_width * feature_height * 64
super(Q, self).__init__(
conv1 = L.Convolution2D(channel, 16, 8, stride=4, pad=3),
conv2 = L.Convolution2D(16, 32, 5, stride=2, pad=2),
conv3 = L.Convolution2D(32, 64, 5, stride=2, pad=2),
lstm = L.LSTM(feature_size, latent_size),
q = L.Linear(latent_size, action_size),
)
self.width = width
self.height = height
self.latent_size = latent_size
def initialize_LSTM(self, LSTM, initializer):
initializers.init_weight(LSTM.upward.W.data, initializer)
initializers.init_weight(LSTM.lateral.W.data, initializer)
def initialize_parameters(self):
G_init = initializers.GlorotNormal()
#initializers.init_weight(self.W_predict.W.data, G_init)
initializers.init_weight(self.W_candidate.W.data, G_init)
self.initialize_LSTM(self.LSTM, G_init)
def solve(self, x_seq, pos, neg, train=True, variablize=False, onebyone=True):
if variablize:# If arguments are just arrays (not variables), make them variables
x_seq = [chainer.Variable(x, volatile=not train) for x in x_seq]
x_seq = [F.dropout(x, ratio=self.dropout_ratio, train=train) for x in x_seq]
pos = self.act1(self.W_candidate(
F.dropout(chainer.Variable(pos, volatile=not train),
ratio=self.dropout_ratio, train=train)))
neg = self.act1(self.W_candidate(
F.dropout(chainer.Variable(neg, volatile=not train),
ratio=self.dropout_ratio, train=train)))
if onebyone and train:
target_x_seq = [self.act1(self.W_candidate(x)) for x in x_seq[:4]]# 1,2,3,4,5-th targets
onebyone_loss = 0.
self.LSTM.reset_state()
for i, x in enumerate(x_seq):
h = self.LSTM( F.dropout(x, ratio=self.dropout_ratio, train=train) )
if onebyone and train and target_x_seq[i+1:]:
pos_score, neg_score = self.calculate_score(h, target_x_seq[i+1:], neg,
multipos=True)
onebyone_loss += F.relu( self.margin - pos_score + neg_score )
pos_score, neg_score = self.calculate_score(h, pos, neg)
accum_loss = F.relu( self.margin - pos_score + neg_score )
TorFs = sum(accum_loss.data < self.margin)
if onebyone and train:
return F.sum(accum_loss) + F.sum(onebyone_loss), TorFs
else:
return F.sum(accum_loss), TorFs
def __init__(self, obs_size, action_size, hidden_size=200, lstm_size=128):
self.pi_head = L.Linear(obs_size, hidden_size)
self.v_head = L.Linear(obs_size, hidden_size)
self.pi_lstm = L.LSTM(hidden_size, lstm_size)
self.v_lstm = L.LSTM(hidden_size, lstm_size)
self.pi = policies.LinearGaussianPolicyWithDiagonalCovariance(
lstm_size, action_size)
self.v = v_function.FCVFunction(lstm_size)
super().__init__(self.pi_head, self.v_head,
self.pi_lstm, self.v_lstm, self.pi, self.v)
def __init__(self, n_dim_obs, n_dim_action, n_hidden_channels,
n_hidden_layers, nonlinearity=F.relu, last_wscale=1.):
self.n_input_channels = n_dim_obs + n_dim_action
self.n_hidden_layers = n_hidden_layers
self.n_hidden_channels = n_hidden_channels
self.nonlinearity = nonlinearity
super().__init__()
with self.init_scope():
self.fc = MLP(self.n_input_channels, n_hidden_channels,
[self.n_hidden_channels] * self.n_hidden_layers,
nonlinearity=nonlinearity,
)
self.lstm = L.LSTM(n_hidden_channels, n_hidden_channels)
self.out = L.Linear(n_hidden_channels, 1,
initialW=LeCunNormal(last_wscale))
def __init__(self, n_input_channels, n_hidden_layers,
n_hidden_channels, action_size,
min_action=None, max_action=None, bound_action=True,
nonlinearity=F.relu,
last_wscale=1.):
self.n_input_channels = n_input_channels
self.n_hidden_layers = n_hidden_layers
self.n_hidden_channels = n_hidden_channels
self.action_size = action_size
self.min_action = min_action
self.max_action = max_action
self.bound_action = bound_action
if self.bound_action:
def action_filter(x):
return bound_by_tanh(
x, self.min_action, self.max_action)
else:
action_filter = None
model = chainer.Chain(
fc=MLP(self.n_input_channels,
n_hidden_channels,
(self.n_hidden_channels,) * self.n_hidden_layers,
nonlinearity=nonlinearity,
),
lstm=L.LSTM(n_hidden_channels, n_hidden_channels),
out=L.Linear(n_hidden_channels, action_size,
initialW=LeCunNormal(last_wscale)),
)
def model_call(model, x):
h = nonlinearity(model.fc(x))
h = model.lstm(h)
h = model.out(h)
return h
super().__init__(
model=model,
model_call=model_call,
action_filter=action_filter)
def __init__(self, n_actions):
self.head = links.NIPSDQNHead()
self.pi = policy.FCSoftmaxPolicy(
self.head.n_output_channels, n_actions)
self.v = v_function.FCVFunction(self.head.n_output_channels)
self.lstm = L.LSTM(self.head.n_output_channels,
self.head.n_output_channels)
super().__init__(self.head, self.lstm, self.pi, self.v)
def __init__(self, obs_size, action_size, hidden_size=200, lstm_size=128):
self.pi_head = L.Linear(obs_size, hidden_size)
self.v_head = L.Linear(obs_size, hidden_size)
self.pi_lstm = L.LSTM(hidden_size, lstm_size)
self.v_lstm = L.LSTM(hidden_size, lstm_size)
self.pi = policies.LinearGaussianPolicyWithDiagonalCovariance(
lstm_size, action_size)
self.v = v_function.FCVFunction(lstm_size)
super().__init__(self.pi_head, self.v_head,
self.pi_lstm, self.v_lstm, self.pi, self.v)
def __init__(self, n_actions):
self.head = dqn_head.NIPSDQNHead()
self.pi = policy.FCSoftmaxPolicy(
self.head.n_output_channels, n_actions)
self.v = v_function.FCVFunction(self.head.n_output_channels)
self.lstm = L.LSTM(self.head.n_output_channels,
self.head.n_output_channels)
super().__init__(self.head, self.lstm, self.pi, self.v)
init_like_torch(self)
def __init__(self, n_actions):
self.head = dqn_head.NIPSDQNHead(n_input_channels=3)
self.pi = policy.FCSoftmaxPolicy(
self.head.n_output_channels, n_actions)
self.v = v_function.FCVFunction(self.head.n_output_channels)
self.lstm = L.LSTM(self.head.n_output_channels,
self.head.n_output_channels)
super().__init__(self.head, self.lstm, self.pi, self.v)
init_like_torch(self)
def __init__(self, deep, gpu, word2index, in_units, hidden_units, out_units, loss_func, train, drop_ratio=0.0):
n_vocab = len(word2index)
l2r_embedding=F.EmbedID(n_vocab, in_units)
r2l_embedding=F.EmbedID(n_vocab, in_units)
if deep:
super(BiLstmContext, self).__init__(
l2r_embed=l2r_embedding,
r2l_embed=r2l_embedding,
loss_func=loss_func,
l2r_1 = L.LSTM(in_units, hidden_units),
r2l_1 = L.LSTM(in_units, hidden_units),
l3 = L.Linear(2*hidden_units, 2*hidden_units),
l4 = L.Linear(2*hidden_units, out_units),
)
else:
super(BiLstmContext, self).__init__(
l2r_embed=l2r_embedding,
r2l_embed=r2l_embedding,
loss_func=loss_func,
l2r_1 = L.LSTM(in_units, hidden_units),
r2l_1 = L.LSTM(in_units, hidden_units),
lp_l2r = L.Linear(hidden_units, out_units/2),
lp_r2l = L.Linear(hidden_units, out_units/2)
)
if gpu >=0:
self.to_gpu()
l2r_embedding.W.data = self.xp.random.normal(0, math.sqrt(1. / l2r_embedding.W.data.shape[0]), l2r_embedding.W.data.shape).astype(np.float32)
r2l_embedding.W.data = self.xp.random.normal(0, math.sqrt(1. / r2l_embedding.W.data.shape[0]), r2l_embedding.W.data.shape).astype(np.float32)
self.word2index = word2index
self.train = train
self.deep = deep
self.drop_ratio = drop_ratio
def __init__(self, n_vocab, n_units):
#n_units = ??????????
super(LSTM, self).__init__(
embed=L.EmbedID(n_vocab, n_units, ignore_label=-1),
l1=L.LSTM(n_units, n_units),
l2=L.Linear(n_units, n_vocab)
)
def __init__(self, n_vocab, n_units, train=True):
super(RNNLM, self).__init__(
embed=L.EmbedID(n_vocab, n_units),
l1=L.LSTM(n_units, n_units),
l2=L.LSTM(n_units, n_units),
l3=L.Linear(n_units, n_vocab),
)
self.train = train
def setUp(self):
self.link = links.LSTM(self.in_size, self.out_size)
upward = self.link.upward.W.data
upward[...] = numpy.random.uniform(-1, 1, upward.shape)
lateral = self.link.lateral.W.data
lateral[...] = numpy.random.uniform(-1, 1, lateral.shape)
self.link.zerograds()
self.upward = upward.copy() # fixed on CPU
self.lateral = lateral.copy() # fixed on CPU
x_shape = (4, self.in_size)
self.x = numpy.random.uniform(-1, 1, x_shape).astype(numpy.float32)
def setUp(self):
self.link = links.LSTM(5, 7)
self.x = chainer.Variable(
numpy.random.uniform(-1, 1, (3, 5)).astype(numpy.float32))
def setUp(self):
self.link = links.LSTM(5, 7)
self.x = chainer.Variable(
numpy.random.uniform(-1, 1, (3, 5)).astype(numpy.float32))
def __init__(self, n_vocab_char, n_units, n_units_char, index2charIds, dropout=.2): #dropout ratio, zero indicates no dropout
super(RNN, self).__init__()
with self.init_scope():
self.embed = L.EmbedID(
n_vocab_char, n_units_char, initialW=I.Uniform(1. / n_units_char)) # word embedding
self.mid = L.LSTM(n_units_char, n_units_char) # the first LSTM layer
self.out = L.Linear(n_units_char, n_units) # the feed-forward output layer
self.dropout = dropout
self.index2charIds = index2charIds
def charRNN(self, context): # input a list of word ids, output a list of word embeddings
# if chainer.config.train:
# print("train")
# else:
# print("test")
contexts2charIds = self.index2charIds[context]
#sorting the context_char, make sure array length in descending order
# ref: https://docs.chainer.org/en/stable/reference/generated/chainer.links.LSTM.html?highlight=Variable-length
context_char_length = np.array([len(t) for t in contexts2charIds])
argsort = context_char_length.argsort()[::-1] # descending order
argsort_reverse = np.zeros(len(argsort), dtype=np.int32) # this is used to restore the original order
for i in range(len(argsort)):
argsort_reverse[argsort[i]] = i
contexts2charIds = contexts2charIds[context_char_length.argsort()[::-1]]
#transpose a 2D list/numpy array
rnn_inputs = [[] for i in range(len(contexts2charIds[0]))]
for j in range(len(contexts2charIds)) :
for i in range(len(contexts2charIds[j])):
rnn_inputs[i].append(contexts2charIds[j][i])
self.reset_state()
for i in range(len(rnn_inputs)):
y_ = self(np.array(rnn_inputs[i], np.int32))
y = self.out(self.mid.h)
y = y[argsort_reverse] # restore the original order
return y
train_word2vec_subword_chainer_input.py 文件源码
项目:vsmlib
作者: undertherain
项目源码
文件源码
阅读 21
收藏 0
点赞 0
评论 0
def __init__(self, n_vocab_char, n_units, n_units_char):
super(RNN, self).__init__()
with self.init_scope():
self.embed = L.EmbedID(
n_vocab_char, n_units_char, initialW=I.Uniform(1. / n_units_char)) # word embedding
self.mid = L.LSTM(n_units_char, n_units_char) # the first LSTM layer
self.out = L.Linear(n_units_char, n_units) # the feed-forward output layer
train_word2vec_subword_chainer_input.py 文件源码
项目:vsmlib
作者: undertherain
项目源码
文件源码
阅读 25
收藏 0
点赞 0
评论 0
def charRNN(self, context): # input a list of word ids, output a list of word embeddings
# if chainer.config.train:
# print("train")
# else:
# print("test")
contexts2charIds = index2charIds[context]
#sorting the context_char, make sure array length in descending order
# ref: https://docs.chainer.org/en/stable/reference/generated/chainer.links.LSTM.html?highlight=Variable-length
context_char_length = np.array([len(t) for t in contexts2charIds])
argsort = context_char_length.argsort()[::-1] # descending order
argsort_reverse = np.zeros(len(argsort), dtype=np.int32) # this is used to restore the original order
for i in range(len(argsort)):
argsort_reverse[argsort[i]] = i
contexts2charIds = contexts2charIds[context_char_length.argsort()[::-1]]
#transpose a 2D list/numpy array
rnn_inputs = [[] for i in range(len(contexts2charIds[0]))]
for j in range(len(contexts2charIds)) :
for i in range(len(contexts2charIds[j])):
rnn_inputs[i].append(contexts2charIds[j][i])
self.reset_state()
for i in range(len(rnn_inputs)):
y_ = self(np.array(rnn_inputs[i], np.int32))
y = self.out(self.mid.h)
y = y[argsort_reverse] # restore the original order
return y
def __init__(self, input_size, output_size):
super(StackedLSTM, self).__init__(
links.LSTM(input_size, output_size),
links.LSTM(output_size, output_size),
#links.LSTM(output_size, output_size),
)
def __init__(self, input_size, output_size):
super(Encoder, self).__init__(
x_f = links.LSTM(input_size, output_size),
x_b = links.LSTM(input_size, output_size),
f_y = links.Linear(output_size, output_size),
b_y = links.Linear(output_size, output_size),
)
def __init__(self, input_size, output_size):
super(Encoder, self).__init__(
x_f = links.LSTM(input_size, output_size),
x_b = links.LSTM(input_size, output_size),
f_y = links.Linear(output_size, output_size),
b_y = links.Linear(output_size, output_size),
)
def __init__(self, input_size, output_size):
super(Encoder, self).__init__(
x_f = links.LSTM(input_size, output_size),
x_b = links.LSTM(input_size, output_size),
f_y = links.Linear(output_size, output_size),
b_y = links.Linear(output_size, output_size),
)
def __init__(self, vocab_size, embed_size):
super(Embed, self).__init__(
c_x = links.EmbedID(0x80, 32),
x_f = links.LSTM(32, embed_size),
x_b = links.LSTM(32, embed_size),
w_e = links.EmbedID(vocab_size, embed_size),
f_e = links.Linear(embed_size, embed_size),
b_e = links.Linear(embed_size, embed_size),
)