循环神经网络
'''
code by Tae Hwan Jung(Jeff Jung) @graykode
'''
import tensorflow as tf
import numpy as np
tf. reset_default_graph( )
sentences = [ "i like dog" , "i love coffee" , "i hate milk" ]
word_list = " " . join( sentences) . split( )
word_list = list ( set ( word_list) )
word_dict = { w: i for i, w in enumerate ( word_list) }
number_dict = { i: w for i, w in enumerate ( word_list) }
n_class = len ( word_dict)
n_step = 2
n_hidden = 5
def make_batch ( sentences) :
input_batch = [ ]
target_batch = [ ]
for sen in sentences:
word = sen. split( )
input = [ word_dict[ n] for n in word[ : - 1 ] ]
target = word_dict[ word[ - 1 ] ]
input_batch. append( np. eye( n_class) [ input ] )
target_batch. append( np. eye( n_class) [ target] )
return input_batch, target_batch
X = tf. placeholder( tf. float32, [ None , n_step, n_class] )
Y = tf. placeholder( tf. float32, [ None , n_class] )
W = tf. Variable( tf. random_normal( [ n_hidden, n_class] ) )
b = tf. Variable( tf. random_normal( [ n_class] ) )
cell = tf. nn. rnn_cell. BasicRNNCell( n_hidden)
outputs, states = tf. nn. dynamic_rnn( cell, X, dtype= tf. float32)
outputs = tf. transpose( outputs, [ 1 , 0 , 2 ] )
outputs = outputs[ - 1 ]
model = tf. matmul( outputs, W) + b
cost = tf. reduce_mean( tf. nn. softmax_cross_entropy_with_logits_v2( logits= model, labels= Y) )
optimizer = tf. train. AdamOptimizer( 0.001 ) . minimize( cost)
prediction = tf. cast( tf. argmax( model, 1 ) , tf. int32)
init = tf. global_variables_initializer( )
sess = tf. Session( )
sess. run( init)
input_batch, target_batch = make_batch( sentences)
for epoch in range ( 5000 ) :
_, loss = sess. run( [ optimizer, cost] , feed_dict= { X: input_batch, Y: target_batch} )
if ( epoch + 1 ) % 1000 == 0 :
print ( 'Epoch:' , '%04d' % ( epoch + 1 ) , 'cost =' , '{:.6f}' . format ( loss) )
input = [ sen. split( ) [ : 2 ] for sen in sentences]
predict = sess. run( [ prediction] , feed_dict= { X: input_batch} )
print ( [ sen. split( ) [ : 2 ] for sen in sentences] , '->' , [ number_dict[ n] for n in predict[ 0 ] ] )
'''
code by Tae Hwan Jung(Jeff Jung) @graykode
'''
import numpy as np
import torch
import torch. nn as nn
import torch. optim as optim
from torch. autograd import Variable
dtype = torch. FloatTensor
sentences = [ "i like dog" , "i love coffee" , "i hate milk" ]
word_list = " " . join( sentences) . split( )
word_list = list ( set ( word_list) )
word_dict = { w: i for i, w in enumerate ( word_list) }
number_dict = { i: w for i, w in enumerate ( word_list) }
n_class = len ( word_dict)
batch_size = len ( sentences)
n_step = 2
n_hidden = 5
def make_batch ( sentences) :
input_batch = [ ]
target_batch = [ ]
for sen in sentences:
word = sen. split( )
input = [ word_dict[ n] for n in word[ : - 1 ] ]
target = word_dict[ word[ - 1 ] ]
input_batch. append( np. eye( n_class) [ input ] )
target_batch. append( target)
return input_batch, target_batch
input_batch, target_batch = make_batch( sentences)
input_batch = Variable( torch. Tensor( input_batch) )
target_batch = Variable( torch. LongTensor( target_batch) )
class TextRNN ( nn. Module) :
def __init__ ( self) :
super ( TextRNN, self) . __init__( )
self. rnn = nn. RNN( input_size= n_class, hidden_size= n_hidden)
self. W = nn. Parameter( torch. randn( [ n_hidden, n_class] ) . type ( dtype) )
self. b = nn. Parameter( torch. randn( [ n_class] ) . type ( dtype) )
def forward ( self, hidden, X) :
X = X. transpose( 0 , 1 )
outputs, hidden = self. rnn( X, hidden)
outputs = outputs[ - 1 ]
model = torch. mm( outputs, self. W) + self. b
return model
model = TextRNN( )
criterion = nn. CrossEntropyLoss( )
optimizer = optim. Adam( model. parameters( ) , lr= 0.001 )
for epoch in range ( 5000 ) :
optimizer. zero_grad( )
hidden = Variable( torch. zeros( 1 , batch_size, n_hidden) )
output = model( hidden, input_batch)
loss = criterion( output, target_batch)
if ( epoch + 1 ) % 1000 == 0 :
print ( 'Epoch:' , '%04d' % ( epoch + 1 ) , 'cost =' , '{:.6f}' . format ( loss) )
loss. backward( )
optimizer. step( )
input = [ sen. split( ) [ : 2 ] for sen in sentences]
hidden = Variable( torch. zeros( 1 , batch_size, n_hidden) )
predict = model( hidden, input_batch) . data. max ( 1 , keepdim= True ) [ 1 ]
print ( [ sen. split( ) [ : 2 ] for sen in sentences] , '->' , [ number_dict[ n. item( ) ] for n in predict. squeeze( ) ] )
3-2. TextLSTM - Autocomplete
TextLSTM-Tensor.py
'''
code by Tae Hwan Jung(Jeff Jung) @graykode
'''
import tensorflow as tf
import numpy as np
tf. reset_default_graph( )
char_arr = [ c for c in 'abcdefghijklmnopqrstuvwxyz' ]
word_dict = { n: i for i, n in enumerate ( char_arr) }
number_dict = { i: w for i, w in enumerate ( char_arr) }
n_class = len ( word_dict)
seq_data = [ 'make' , 'need' , 'coal' , 'word' , 'love' , 'hate' , 'live' , 'home' , 'hash' , 'star' ]
n_step = 3
n_hidden = 128
def make_batch ( seq_data) :
input_batch, target_batch = [ ] , [ ]
for seq in seq_data:
input = [ word_dict[ n] for n in seq[ : - 1 ] ]
target = word_dict[ seq[ - 1 ] ]
input_batch. append( np. eye( n_class) [ input ] )
target_batch. append( np. eye( n_class) [ target] )
return input_batch, target_batch
X = tf. placeholder( tf. float32, [ None , n_step, n_class] )
Y = tf. placeholder( tf. float32, [ None , n_class] )
W = tf. Variable( tf. random_normal( [ n_hidden, n_class] ) )
b = tf. Variable( tf. random_normal( [ n_class] ) )
cell = tf. nn. rnn_cell. BasicLSTMCell( n_hidden)
outputs, states = tf. nn. dynamic_rnn( cell, X, dtype= tf. float32)
outputs = tf. transpose( outputs, [ 1 , 0 , 2 ] )
outputs = outputs[ - 1 ]
model = tf. matmul( outputs, W) + b
cost = tf. reduce_mean( tf. nn. softmax_cross_entropy_with_logits_v2( logits= model, labels= Y) )
optimizer = tf. train. AdamOptimizer( 0.001 ) . minimize( cost)
prediction = tf. cast( tf. argmax( model, 1 ) , tf. int32)
init = tf. global_variables_initializer( )
sess = tf. Session( )
sess. run( init)
input_batch, target_batch = make_batch( seq_data)
for epoch in range ( 1000 ) :
_, loss = sess. run( [ optimizer, cost] , feed_dict= { X: input_batch, Y: target_batch} )
if ( epoch + 1 ) % 100 == 0 :
print ( 'Epoch:' , '%04d' % ( epoch + 1 ) , 'cost =' , '{:.6f}' . format ( loss) )
inputs = [ sen[ : 3 ] for sen in seq_data]
predict = sess. run( [ prediction] , feed_dict= { X: input_batch} )
print ( inputs, '->' , [ number_dict[ n] for n in predict[ 0 ] ] )
TextLSTM-Torch.py
'''
code by Tae Hwan Jung(Jeff Jung) @graykode
'''
import numpy as np
import torch
import torch. nn as nn
import torch. optim as optim
from torch. autograd import Variable
dtype = torch. FloatTensor
char_arr = [ c for c in 'abcdefghijklmnopqrstuvwxyz' ]
word_dict = { n: i for i, n in enumerate ( char_arr) }
number_dict = { i: w for i, w in enumerate ( char_arr) }
n_class = len ( word_dict)
seq_data = [ 'make' , 'need' , 'coal' , 'word' , 'love' , 'hate' , 'live' , 'home' , 'hash' , 'star' ]
n_step = 3
n_hidden = 128
def make_batch ( seq_data) :
input_batch, target_batch = [ ] , [ ]
for seq in seq_data:
input = [ word_dict[ n] for n in seq[ : - 1 ] ]
target = word_dict[ seq[ - 1 ] ]
input_batch. append( np. eye( n_class) [ input ] )
target_batch. append( target)
return Variable( torch. Tensor( input_batch) ) , Variable( torch. LongTensor( target_batch) )
class TextLSTM ( nn. Module) :
def __init__ ( self) :
super ( TextLSTM, self) . __init__( )
self. lstm = nn. LSTM( input_size= n_class, hidden_size= n_hidden)
self. W = nn. Parameter( torch. randn( [ n_hidden, n_class] ) . type ( dtype) )
self. b = nn. Parameter( torch. randn( [ n_class] ) . type ( dtype) )
def forward ( self, X) :
input = X. transpose( 0 , 1 )
hidden_state = Variable( torch. zeros( 1 , len ( X) , n_hidden) )
cell_state = Variable( torch. zeros( 1 , len ( X) , n_hidden) )
outputs, ( _, _) = self. lstm( input , ( hidden_state, cell_state) )
outputs = outputs[ - 1 ]
model = torch. mm( outputs, self. W) + self. b
return model
input_batch, target_batch = make_batch( seq_data)
model = TextLSTM( )
criterion = nn. CrossEntropyLoss( )
optimizer = optim. Adam( model. parameters( ) , lr= 0.001 )
output = model( input_batch)
for epoch in range ( 1000 ) :
optimizer. zero_grad( )
output = model( input_batch)
loss = criterion( output, target_batch)
if ( epoch + 1 ) % 100 == 0 :
print ( 'Epoch:' , '%04d' % ( epoch + 1 ) , 'cost =' , '{:.6f}' . format ( loss) )
loss. backward( )
optimizer. step( )
inputs = [ sen[ : 3 ] for sen in seq_data]
predict = model( input_batch) . data. max ( 1 , keepdim= True ) [ 1 ]
print ( inputs, '->' , [ number_dict[ n. item( ) ] for n in predict. squeeze( ) ] )
'''
code by Tae Hwan Jung(Jeff Jung) @graykode
'''
import tensorflow as tf
import numpy as np
tf. reset_default_graph( )
sentence = (
'Lorem ipsum dolor sit amet consectetur adipisicing elit '
'sed do eiusmod tempor incididunt ut labore et dolore magna '
'aliqua Ut enim ad minim veniam quis nostrud exercitation'
)
word_dict = { w: i for i, w in enumerate ( list ( set ( sentence. split( ) ) ) ) }
number_dict = { i: w for i, w in enumerate ( list ( set ( sentence. split( ) ) ) ) }
n_class = len ( word_dict)
n_step = len ( sentence. split( ) )
n_hidden = 5
def make_batch ( sentence) :
input_batch = [ ]
target_batch = [ ]
words = sentence. split( )
for i, word in enumerate ( words[ : - 1 ] ) :
input = [ word_dict[ n] for n in words[ : ( i + 1 ) ] ]
input = input + [ 0 ] * ( n_step - len ( input ) )
target = word_dict[ words[ i + 1 ] ]
input_batch. append( np. eye( n_class) [ input ] )
target_batch. append( np. eye( n_class) [ target] )
return input_batch, target_batch
X = tf. placeholder( tf. float32, [ None , n_step, n_class] )
Y = tf. placeholder( tf. float32, [ None , n_class] )
W = tf. Variable( tf. random_normal( [ n_hidden * 2 , n_class] ) )
b = tf. Variable( tf. random_normal( [ n_class] ) )
lstm_fw_cell = tf. nn. rnn_cell. LSTMCell( n_hidden)
lstm_bw_cell = tf. nn. rnn_cell. LSTMCell( n_hidden)
outputs, _ = tf. nn. bidirectional_dynamic_rnn( lstm_fw_cell, lstm_bw_cell, X, dtype= tf. float32)
outputs = tf. concat( [ outputs[ 0 ] , outputs[ 1 ] ] , 2 )
outputs = tf. transpose( outputs, [ 1 , 0 , 2 ] )
outputs = outputs[ - 1 ]
model = tf. matmul( outputs, W) + b
cost = tf. reduce_mean( tf. nn. softmax_cross_entropy_with_logits_v2( logits= model, labels= Y) )
optimizer = tf. train. AdamOptimizer( 0.001 ) . minimize( cost)
prediction = tf. cast( tf. argmax( model, 1 ) , tf. int32)
init = tf. global_variables_initializer( )
sess = tf. Session( )
sess. run( init)
input_batch, target_batch = make_batch( sentence)
for epoch in range ( 10000 ) :
_, loss = sess. run( [ optimizer, cost] , feed_dict= { X: input_batch, Y: target_batch} )
if ( epoch + 1 ) % 1000 == 0 :
print ( 'Epoch:' , '%04d' % ( epoch + 1 ) , 'cost =' , '{:.6f}' . format ( loss) )
predict = sess. run( [ prediction] , feed_dict= { X: input_batch} )
print ( sentence)
print ( [ number_dict[ n] for n in [ pre for pre in predict[ 0 ] ] ] )
Bi-LSTM-Torch.py
'''
code by Tae Hwan Jung(Jeff Jung) @graykode
'''
import numpy as np
import torch
import torch. nn as nn
import torch. optim as optim
from torch. autograd import Variable
import torch. nn. functional as F
dtype = torch. FloatTensor
sentence = (
'Lorem ipsum dolor sit amet consectetur adipisicing elit '
'sed do eiusmod tempor incididunt ut labore et dolore magna '
'aliqua Ut enim ad minim veniam quis nostrud exercitation'
)
word_dict = { w: i for i, w in enumerate ( list ( set ( sentence. split( ) ) ) ) }
number_dict = { i: w for i, w in enumerate ( list ( set ( sentence. split( ) ) ) ) }
n_class = len ( word_dict)
max_len = len ( sentence. split( ) )
n_hidden = 5
def make_batch ( sentence) :
input_batch = [ ]
target_batch = [ ]
words = sentence. split( )
for i, word in enumerate ( words[ : - 1 ] ) :
input = [ word_dict[ n] for n in words[ : ( i + 1 ) ] ]
input = input + [ 0 ] * ( max_len - len ( input ) )
target = word_dict[ words[ i + 1 ] ]
input_batch. append( np. eye( n_class) [ input ] )
target_batch. append( target)
return Variable( torch. Tensor( input_batch) ) , Variable( torch. LongTensor( target_batch) )
class BiLSTM ( nn. Module) :
def __init__ ( self) :
super ( BiLSTM, self) . __init__( )
self. lstm = nn. LSTM( input_size= n_class, hidden_size= n_hidden, bidirectional= True )
self. W = nn. Parameter( torch. randn( [ n_hidden * 2 , n_class] ) . type ( dtype) )
self. b = nn. Parameter( torch. randn( [ n_class] ) . type ( dtype) )
def forward ( self, X) :
input = X. transpose( 0 , 1 )
hidden_state = Variable( torch. zeros( 1 * 2 , len ( X) , n_hidden) )
cell_state = Variable( torch. zeros( 1 * 2 , len ( X) , n_hidden) )
outputs, ( _, _) = self. lstm( input , ( hidden_state, cell_state) )
outputs = outputs[ - 1 ]
model = torch. mm( outputs, self. W) + self. b
return model
input_batch, target_batch = make_batch( sentence)
model = BiLSTM( )
criterion = nn. CrossEntropyLoss( )
optimizer = optim. Adam( model. parameters( ) , lr= 0.001 )
for epoch in range ( 10000 ) :
optimizer. zero_grad( )
output = model( input_batch)
loss = criterion( output, target_batch)
if ( epoch + 1 ) % 1000 == 0 :
print ( 'Epoch:' , '%04d' % ( epoch + 1 ) , 'cost =' , '{:.6f}' . format ( loss) )
loss. backward( )
optimizer. step( )
predict = model( input_batch) . data. max ( 1 , keepdim= True ) [ 1 ]
print ( sentence)
print ( [ number_dict[ n. item( ) ] for n in predict. squeeze( ) ] )