如何打印TensorVariable

时间:2016-09-27 13:14:18

标签: theano theano.scan

我是theano的初学者。我正在研究它。我想打印“价值”'和#'塑造'在操作theano.function时的TensorVariable。当我使用python的print函数时,打印函数在编译theano函数之前运行。所以我学会了使用print功能是没用的。我试着另一只手。我添加了以下语法来执行theano.printing.Print。

(cce是theano.scan的返回值。因此,也许它不是符号可变的。 实际上,我对TensorVariable和共享变量的概念感到困惑。 TensorVariable是一种共享变量?)

  x = theano.tensor.tensor3() # define data type
  t_print =theano.printing.Print("cce value is : ")(x)
  f = theano.function([x], t_print) # define theano.function
  f(cce) # call f (print value of cce)

然后,出现以下错误

  TypeError: ('Bad input argument to theano function with name "seq2seq.py : 98"  at index 0(0-based)', 'Expected an array-like object, but found a Variable: maybe you are trying to call a function on a (possibly shared) variable instead of a numeric array?')

您是否可以让我知道如何更正此代码以打印cce(TensorVariable)的值?或者,当theano.function正在进行时,是否无法打印TensorVariable的值?

感谢您阅读我的问题。

  • 已添加 -

这是我的源代码。这是一幅大图。 theano.function()从最后一行开始。 loss_func是" categorical_crossentropy function'。最后4行是关于theano功能

def categorical_crossentropy(y_true, y_pred):
    y_pred = T.clip(y_pred, epsilon, 1.0 - epsilon)
    y_pred = y_pred.reshape( (-1, voca_dim_g) )

    y_true = y_true.reshape( (-1, voca_dim_g) )   

    cce, updates = theano.scan(
    fn=T.nnet.categorical_crossentropy,   
        sequences=[y_pred,y_true]
    )    
    ##### I want to print cce HERE #######
    return T.mean(cce)

    @staticmethod
def step(
    x_t, h_tm1, c_tm1,
    Ui, Wi, bi, Uf, Wf, bf,
    Uo, Wo, bo, Ug, Wg, bg
):

    """
    x_t.shape = (timestep=1, dim)
    x_t.shape = (n_samples, timestep=1, dim)

    """

    i_t = T.nnet.sigmoid(T.dot(x_t, Ui) + T.dot(h_tm1, Wi) + bi)
    f_t = T.nnet.sigmoid(T.dot(x_t, Uf) + T.dot(h_tm1, Wf) + bf)
    o_t = T.nnet.sigmoid(T.dot(x_t, Uo) + T.dot(h_tm1, Wo) + bo)
    g_t = T.tanh(T.dot(x_t, Ug) + T.dot(h_tm1, Wg) + bg)

    c_t = c_tm1 * f_t + g_t * i_t
    h_t = T.tanh(c_t) * o_t

    return h_t, c_t

#########################################################################################################################

def forward(self, X):

    states, updates = theano.scan(
        fn=self.step,
        sequences=[  X  ],
        outputs_info=[self.h_tm1, self.c_tm1],
        non_sequences=[
            self.Ui, self.Wi, self.bi,
            self.Uf, self.Wf, self.bf,
            self.Uo, self.Wo, self.bo,
            self.Ug, self.Wg, self.bg
        ]
    )

    updates = [(self.h_tm1, states[0][-1]), (self.c_tm1, states[1][-1])]
    return states, updates

#########################################################################################################################
def encode(self, X):
    states, updates = self.forward(X)
    h_t = states[0][-1]
    c_t = states[1][-1]
    return h_t, c_t, updates



def decode_step(
    self, y_t, h_tm1, c_tm1,
    Ui, Wi, bi, Uf, Wf, bf,
    Uo, Wo, bo, Ug, Wg, bg,
    Wh, bh
):
    h_t, c_t = self.step(
        y_t, h_tm1, c_tm1,
        Ui, Wi, bi, Uf, Wf, bf,
        Uo, Wo, bo, Ug, Wg, bg
    )
    y_t = T.dot(h_t, Wh) + bh
    return y_t, h_t, c_t

def decode(self, h_tm1, c_tm1, timesteps):

    outputs, updates = theano.scan(
        fn=self.decode_step,
        outputs_info=[self.y_t, h_tm1, c_tm1],
        non_sequences=[
            self.Ui, self.Wi, self.bi,
            self.Uf, self.Wf, self.bf,
            self.Uo, self.Wo, self.bo,
            self.Ug, self.Wg, self.bg,
            self.Wh, self.bh
        ],
        n_steps=timesteps
    )
    updates = [
        (self.h_tm1, outputs[1][-1]),
        (self.c_tm1, outputs[2][-1])
    ]
    return outputs[0], updates

    h_tm1, c_tm1, updates_encode = encode(seq_input)
    seq_predict, updates_decode = decode(h_tm1, c_tm1, T.shape(seq_target)[0])
    loss = loss_func(seq_predict, seq_target)
    self._train = theano.function([seq_input, seq_target], loss, updates = updates)

下面是完整的源代码

# -*- coding: utf-8 -*-

__modifier__ = "Lee Guk Beom, Lee Jae Sang, Jang Jae Kwang (alphabetical Order)"

import readFile
import numpy as np
import theano
import theano.tensor as T
from six.moves import zip
from theano.compile.debugmode import DebugMode
import nltk
import sys
import os
from nltk.tokenize import sent_tokenize
import codecs

#theano.config.optimizer='fast_compile'
#theano.config.exception_verbosity='high'
#theano.config.compute_test_value = 'warn'

epsilon = 1e-6
dtype = theano.config.floatX
minibatch_size_g = 0
longest_seq_g = 0
voca_dim_g = 0
n_time_step_input_g = 0
n_timestep_target_g = 0
word_to_index_input_g = dict()
word_to_index_targrt_g = dict()
index_to_word_target_g = dict()
#########################################################################################################################

def shared(value, name=None):
    return theano.shared(value.astype(dtype), name=name)

#########################################################################################################################

def shared_zeros(shape, name=None):
    return shared(value=np.zeros(shape), name=name)

#########################################################################################################################

def shared_zeros_like(x, name=None):
    return shared_zeros(shape=x.shape, name=name)

#########################################################################################################################

def init_weights(shape, name=None):
    bound = np.sqrt(1.0/shape[1])
    w = np.random.uniform(-bound, bound, shape)
    return shared(value=w, name=name)

#########################################################################################################################

def adadelta(params, cost, lr=1.0, rho=0.95):
    # from https://github.com/fchollet/keras/blob/master/keras/optimizers.py
    cost = cost.astype('float32')
    grads = T.grad(cost, params)
    accus = [shared_zeros_like(p.get_value()) for p in params]
    delta_accus = [shared_zeros_like(p.get_value()) for p in params]
    updates = []
    for p, g, a, d_a in zip(params, grads, accus, delta_accus):
        new_a = rho * a + (1.0 - rho) * T.square(g)
        updates.append((a, new_a))
        update = g * T.sqrt(d_a + epsilon) / T.sqrt(new_a + epsilon)
        new_p = p - lr * update
        updates.append((p, new_p))
        new_d_a = rho * d_a + (1.0 - rho) * T.square(update)
        updates.append((d_a, new_d_a))
    return updates

#########################################################################################################################

def categorical_crossentropy(y_true, y_pred):
    # from https://github.com/fchollet/keras/blob/master/keras/objectives.py
    y_pred = T.clip(y_pred, epsilon, 1.0 - epsilon)

#    y_true = y_true.reshape( (-1, minibatch_size_g, voca_dim_g) )

    '''
    cce = T.nnet.categorical_crossentropy(y_pred,y_true)
    # only matrix can be calculated
    '''

    # Y_PRED SOFTMAX

    y_pred = y_pred.reshape( (-1, voca_dim_g) )
#    y_pred_flat = T.nnet.softmax(y_pred)

    y_true = y_true.reshape( (-1, voca_dim_g) )   

    cce, updates = theano.scan(
    fn=T.nnet.categorical_crossentropy,   
        sequences=[y_pred,y_true]
    )    

    return T.mean(cce)

#########################################################################################################################

def mean_square_error(y_true, y_pred):
    return T.mean(T.square(y_pred - y_true))

#########################################################################################################################

class LSTM(object):
    def __init__(self, size, dim):

        self.size = size
        self.dim = dim

        shape_b = (minibatch_size_g, size)
        shape_U = (dim, size)
        shape_W = (size, size)

        self.h_tm1 = shared_zeros(shape_b, "h_tm1")
        self.c_tm1 = shared_zeros(shape_b, "c_tm1")

        self.Ui = init_weights(shape_U, "Ui")
        self.Wi = init_weights(shape_W, "Wi")
        self.bi = shared_zeros(shape_b, "bi")

        self.Uf = init_weights(shape_U, "Uf")
        self.Wf = init_weights(shape_W, "Wf")
        self.bf = shared_zeros(shape_b, "bf")

        self.Uo = init_weights(shape_U, "Uo")
        self.Wo = init_weights(shape_W, "Wo")
        self.bo = shared_zeros(shape_b, "bo")

        self.Ug = init_weights(shape_U, "Ug")
        self.Wg = init_weights(shape_W, "Wg")
        self.bg = shared_zeros(shape_b, "bg")

        self.params = [
            self.Ui, self.Wi, self.bi,
            self.Uf, self.Wf, self.bf,
            self.Uo, self.Wo, self.bo,
            self.Ug, self.Wg, self.bg
        ]

    def set_state(self, h, c):
        self.h_tm1.set_value(h.get_value())
        self.c_tm1.set_value(c.get_value())

    def reset_state(self):
        self.h_tm1 = shared_zeros((1, self.size), "h_tm1")
        self.c_tm1 = shared_zeros((1, self.size), "c_tm1")

#########################################################################################################################

    @staticmethod
    def step(
        x_t, h_tm1, c_tm1,
        Ui, Wi, bi, Uf, Wf, bf,
        Uo, Wo, bo, Ug, Wg, bg
    ):

        """
        x_t.shape = (timestep=1, dim)
        x_t.shape = (n_samples, timestep=1, dim)

        """

        i_t = T.nnet.sigmoid(T.dot(x_t, Ui) + T.dot(h_tm1, Wi) + bi)
        f_t = T.nnet.sigmoid(T.dot(x_t, Uf) + T.dot(h_tm1, Wf) + bf)
        o_t = T.nnet.sigmoid(T.dot(x_t, Uo) + T.dot(h_tm1, Wo) + bo)
        g_t = T.tanh(T.dot(x_t, Ug) + T.dot(h_tm1, Wg) + bg)

        c_t = c_tm1 * f_t + g_t * i_t
        h_t = T.tanh(c_t) * o_t

        return h_t, c_t

#########################################################################################################################

    def forward(self, X):

        states, updates = theano.scan(
            fn=self.step,
            sequences=[  X  ],
            outputs_info=[self.h_tm1, self.c_tm1],
            non_sequences=[
                self.Ui, self.Wi, self.bi,
                self.Uf, self.Wf, self.bf,
                self.Uo, self.Wo, self.bo,
                self.Ug, self.Wg, self.bg
            ]
        )

        updates = [(self.h_tm1, states[0][-1]), (self.c_tm1, states[1][-1])]
        return states, updates

#########################################################################################################################

class LSTMEncoder(LSTM):
    def encode(self, X):
        states, updates = self.forward(X)
        h_t = states[0][-1]
        c_t = states[1][-1]
        return h_t, c_t, updates


class LSTMDecoder(LSTM):
    def __init__(self, size, dim, h_tm1=None, c_tm1=None):
        super(LSTMDecoder, self).__init__(size=size, dim=dim)
        self.Wh = init_weights((size, dim), "Wh")
        self.bh = shared_zeros((minibatch_size_g, dim), "bh")

        self.h_tm1 = h_tm1 or shared_zeros((minibatch_size_g, size), "h_tm1")
        self.c_tm1 = c_tm1 or shared_zeros((minibatch_size_g, size), "c_tm1")

        self.y_t = shared_zeros((minibatch_size_g, dim), "y_t")

        # self.decode_length = theano.shared(decode_length)

        self.params.append(self.Wh)
        self.params.append(self.bh)

    def decode_step(
        self, y_t, h_tm1, c_tm1,
        Ui, Wi, bi, Uf, Wf, bf,
        Uo, Wo, bo, Ug, Wg, bg,
        Wh, bh
    ):
        h_t, c_t = self.step(
            y_t, h_tm1, c_tm1,
            Ui, Wi, bi, Uf, Wf, bf,
            Uo, Wo, bo, Ug, Wg, bg
        )
        y_t = T.dot(h_t, Wh) + bh
        return y_t, h_t, c_t

    def decode(self, h_tm1, c_tm1, timesteps):

        outputs, updates = theano.scan(
            fn=self.decode_step,
            outputs_info=[self.y_t, h_tm1, c_tm1],
            non_sequences=[
                self.Ui, self.Wi, self.bi,
                self.Uf, self.Wf, self.bf,
                self.Uo, self.Wo, self.bo,
                self.Ug, self.Wg, self.bg,
                self.Wh, self.bh
            ],
            n_steps=timesteps
        )
        updates = [
            (self.h_tm1, outputs[1][-1]),
            (self.c_tm1, outputs[2][-1])
        ]
#        return T.flatten(outputs[0], 3), updates
        return outputs[0], updates

    @staticmethod
    def argmax(seq):
        seq = T.argmax(seq, axis=2)
        return seq

#########################################################################################################################

class Seq2Seq(object):
    def __init__(self, size, dim):
        self.encoder = LSTMEncoder(size, dim)
        self.decoder = LSTMDecoder(size, dim)
        self.params = []
        self.params += self.encoder.params
        self.params += self.decoder.params
        self._predict = None
        self._train = None
        self._test = None

    def compile(self, loss_func, optimizer):
        seq_input = T.tensor3()
        seq_target = T.tensor3()
        decode_timesteps = T.iscalar()

        h_tm1, c_tm1, updates_encode = self.encoder.encode(seq_input)
        seq_predict_flex, updates_decode_flex = self.decoder.decode(h_tm1, c_tm1, decode_timesteps)
        seq_argmax = self.decoder.argmax(seq_predict_flex)

        seq_predict, updates_decode = self.decoder.decode(h_tm1, c_tm1, T.shape(seq_target)[0])

        loss = loss_func(seq_predict, seq_target)

        self._predict = theano.function([seq_input, decode_timesteps], seq_argmax, updates=updates_encode+updates_decode_flex)
        self._test = theano.function([seq_input, seq_target], loss, updates=updates_encode+updates_decode)
        updates = []
        updates += updates_encode
        updates += updates_decode
        updates += optimizer(self.params, loss)
        self._train = theano.function([seq_input, seq_target], loss, updates = updates)


    def predict(self, seq_input, decode_timesteps):
        self.encoder.reset_state()
        self.decoder.reset_state()
        return self._predict(seq_input, decode_timesteps)

    def train(self, seq_input, seq_target):
        self.encoder.reset_state()
        self.decoder.reset_state()
        return self._train(seq_input, seq_target)

    def test(self, seq_input, seq_target):
        self.encoder.reset_state()
        self.decoder.reset_state()
        return self._test(seq_input, seq_target)

#########################################################################################################################

def train(x, target):
    for mini_batch, target in zip(x,target):
        mini_batch = mini_batch.astype(dtype)
        target = target.astype(dtype)

        print("result of train function(loss or update) :", seq2seq.train(mini_batch, target))


#########################################################################################################################

# make weight information to pickle file
# information of Encooder class and decoder class of Seq2Seq class
# Encooder and decoder class should have function that returns value of weight variables
# one list contains elements that save weights' information
def save_weight():
    None


#########################################################################################################################

def gen_processed_seq(input_sentence):
    tokenized_seq = nltk.word_tokenize( input_sentence )

    input_sentences = [ None for _ in range(1) ]
    input_sentences[0] = tokenized_seq

    seq_input = readFile.word_to_idx(input_sentences, word_to_index_input_g )

    sorted_seq_input = [ None for _ in range(minibatch_size_g) ]
    sorted_seq_input[0] = seq_input[0]

    input_len = len(seq_input[0])

    for i in range(minibatch_size_g-1):
        for j in range(input_len):
            sorted_seq_input[i+1] = [-1]

    input_finally = [] 
    input_finally.append(sorted_seq_input)
    return input_finally


#########################################################################################################################

def gen_one_hot(input_len, input_seq):
    one_hot = readFile.seq_to_1hot(n_time_step_input_g, input_seq, "predict", 1, 1)
    one_hot[0] = one_hot[0].astype(dtype)

    print("one_hot : ", one_hot)
    return one_hot


def get_idx(argmax, num_of_word):
    idx_list = argmax[ : num_of_word, 0]
    return idx_list

#########################################################################################################################

def predict():
    input_sentence = raw_input("Input the English Sentence You Want to Translate into Spanish : ")
    input_seq = gen_processed_seq(input_sentence)
    print("input_seq[0][0] : ",input_seq[0][0])
    num_of_word = len(input_seq[0][0])

    one_hot = gen_one_hot(n_time_step_input_g, input_seq)

    argmax = seq2seq.predict(one_hot[0] , n_time_step_input_g )
    print("argmax_fin shape : ", argmax.shape)
    print("argmax_fin : ", argmax)

    idx_list_np = get_idx(argmax, num_of_word)
    idx_list_py = idx_list_np.tolist()
    print("index_to_word_target_g : ",index_to_word_target_g)
    print("index_to_word_target_g[6] :", index_to_word_target_g[6])
    result = readFile.idx_to_word(idx_list_py, index_to_word_target_g)
    translated = ""

    for elem in result :
        translated += elem
        translated += " "

    print("translated : " , translated)
    print("Translation End")

#########################################################################################################################

def gen_global_var(word_to_index_input, word_to_index_targrt, voca_dim, si, st, index_to_word_target):
    global word_to_index_input_g
    global word_to_index_targrt_g
    global voca_dim_g
    global minibatch_size_g
    global n_time_step_input_g
    global n_timestep_target_g
    global index_to_word_target_g

    word_to_index_input_g = word_to_index_input
    word_to_index_targrt_g = word_to_index_targrt
    voca_dim_g = voca_dim + 2
    minibatch_size_g = si[0].shape[1]
    n_time_step_input_g = si[0].shape[0]
    n_timestep_target_g = st[0].shape[0]
    index_to_word_target_g = index_to_word_target

    return


#########################################################################################################################

def menu(si, st): 
    None


#########################################################################################################################

def gen_object():
    return None


#########################################################################################################################

if __name__ == "__main__":
    si, st, maxlen_input, minibatch_size, voca_dim, word_to_index_input, word_to_index_targrt, index_to_word_target = readFile.preprocessing()

    gen_global_var(word_to_index_input, word_to_index_targrt, voca_dim, si, st, index_to_word_target)

    seq2seq = Seq2Seq(n_time_step_input_g, voca_dim_g )
    seq2seq.compile(loss_func=categorical_crossentropy, optimizer=adadelta)

    while(True):
        print("select a menu")
        print("1. Training")
        print("2. Translate specific English sentence into Spanish.")
        val = input("selection : ") 

        if val == 1:
        train(si, st)

        elif val == 2:
        predict()

和readfile.py是

import numpy as np
import itertools
import nltk
import sys
import os
from nltk.tokenize import sent_tokenize
import codecs

unknown_token = 'UNKNOWN_TOKEN'
start_token  = '_S'
end_token  = '__E'
num_of_seq = 0

input_path = "./europarl-v7.es-en.en"
target_path = "./europarl-v7.es-en.es"

minibatch_unit = 100
voca_dim = 3000
SEQ_NUM_LIMIT = 1000

##########################################################################################

def file_tokenize(file):
    f = codecs.open( file, "r", "utf-8" )
    tokenized_seq = []
    sentences = []

    total_sentence_num = 0
    # sequence tokenize
    for i,line in enumerate(f):
        print("tokenized Sentence No." , i)
        #  strip() method to remove the newline character at the end of the input line.
        tokenized_seq = nltk.word_tokenize( line.strip() )
        tokenized_seq.insert(0, start_token)
        tokenized_seq.append(end_token)
        sentences.append(tokenized_seq)

        total_sentence_num += 1;
        if(total_sentence_num == SEQ_NUM_LIMIT):
            break
    return sentences,total_sentence_num

##########################################################################################

# Count the word frequencies
def cntWordFreq(sentences):
    word_freq = nltk.FreqDist(itertools.chain(*sentences))
    return word_freq

##########################################################################################

# Get the most common words and build index_to_word and word_to_index vectors
def build_WordToIdx_IdxtoWord(word_freq):

    vocab = word_freq.most_common(voca_dim-1)
    index_to_word = [x[0] for x in vocab]
    index_to_word.append(unknown_token)

    word_to_index = dict([(w,i) for i,w in enumerate(index_to_word)])
    return index_to_word, word_to_index

##########################################################################################

# change word to index
def word_to_idx(sequences, word_to_index ) :
    for i, sent in enumerate(sequences):
        sequences[i] = [w if w in word_to_index else unknown_token for w in sent]
        sequences[i] = [word_to_index[w] if w in word_to_index else -1 for w in sequences[i]]

    return sequences

##########################################################################################

def idx_to_word(seq, index_to_word):
    for i, sent in enumerate(seq):
        seq[i] = index_to_word[sent]
        #seq[i] = [index_to_word[sent] if sent in index_to_word else '?' ]

    return seq
##########################################################################################

def sortByLen(seqs_input, seqs_target) :
    # check maximum sentence length
    max_len_input = 0
    max_len_target = 0

    for sentence in seqs_input :
        tmp = len(sentence)
        if  max_len_input < tmp:
            max_len_input = tmp

    for sentence in seqs_target :
        tmp = len(sentence)
        if  max_len_target < tmp:
            max_len_target = tmp

    seqs_sorted_input = [ [] for _ in range(max_len_input+1) ]
    seqs_sorted_target = [ [] for _ in range(max_len_input+1) ]

    i = 0
    for sentence_input, sentence_target in zip(seqs_input, seqs_target) :
        sentence_len = len(sentence_input)
        seqs_sorted_input[sentence_len].append(sentence_input)
        seqs_sorted_target[sentence_len].append(sentence_target)
        i+=1

    return seqs_sorted_input, seqs_sorted_target, max_len_input, max_len_target

##########################################################################################

def find_maxlen(sentence_group):
    max_seq_len = 0
    for seq in sentence_group :
        if len(seq) > max_seq_len :
            max_seq_len = len(seq)
    return max_seq_len

##########################################################################################

def sort_by_timestep(sentence_group):
    same_len_seq = np.asarray(sentence_group)

    same_len_seq = apply_to_m1(same_len_seq)
    sorted_seq = same_len_seq.transpose()

    return sorted_seq

##########################################################################################

def seq_to_1hot(max_len, sorted_sentences, type, minibatch_unit, num_of_seq):
    one_hot = [None for _ in range( len(sorted_sentences) )]

    for i, sentence_group in enumerate(sorted_sentences):

        if sentence_group and len(sentence_group[0]) != 0 :

            max_seq_len = find_maxlen(sentence_group)            
            row = max_seq_len * minibatch_unit
            one_hot[i] = np.zeros( (row, voca_dim + 2) )
            time_step_seq = sort_by_timestep(sentence_group)

            j = 0

            for word_idx in np.nditer( time_step_seq ) :
                if word_idx != -1:
                    one_hot[i][j][word_idx] = 1
                j+=1

            one_hot[i] = np.reshape(one_hot[i], ( max_seq_len, -1, voca_dim+2)  )
    return one_hot

##########################################################################################

def apply_to_m1(lst, dtype=np.int64):
    inner_max_len = max(map(len, lst))
    result = np.zeros(  [len(lst), inner_max_len], dtype  )
    result[:] = -1

    for i, row in enumerate(lst):
        for j, val in enumerate(row):
            result[i][j] = val
    return result

##########################################################################################

def seq_group_by_mini_batch_size(minibatch_unit, sorted_seq, num_of_seq):
    idx = 0
    cnt = 0

    minibatch_seq = [  [] for _ in range( (num_of_seq/minibatch_unit)+1) ]

    for seqs in sorted_seq :
        if seqs :
            for seq in seqs :
                if seq:
                    minibatch_seq[idx].append(seq)
                    cnt+=1
                    if minibatch_unit == cnt:  
                        cnt = 0
                        idx+= 1



    for i, seq in enumerate (minibatch_seq):
        if seq == []:
            minibatch_seq = minibatch_seq[: i- 1]
            break

    return minibatch_seq

##########################################################################################

def preprocessing():

    global num_of_seq
    global minibatch_unit
    global input_path
    global target_path

    print("Start Preprocessing")

    sentences_input, total_sentence_num = file_tokenize(input_path)
    sentences_target, total_sentence_num_target = file_tokenize(target_path)
    print("FINISHED : file_tokenize ")
    word_freq_input = cntWordFreq(sentences_input)
    word_freq_target = cntWordFreq(sentences_target)
    print("FINISHED : cntWordFreq ")

    index_to_word_input, word_to_index_input = build_WordToIdx_IdxtoWord(word_freq_input)
    index_to_word_target, word_to_index_targrt = build_WordToIdx_IdxtoWord(word_freq_target)
    print("FINISHED : build_WordToIdx_IdxtoWord ")
    seqs_input = word_to_idx(sentences_input, word_to_index_input)
    seqs_target = word_to_idx(sentences_target, word_to_index_targrt)
    print("FINISHED : word_to_idx ")
    seqs_sorted_input, seqs_sorted_target, maxlen_input, maxlen_target = sortByLen(seqs_input, seqs_target)
    print("FINISHED : sortByLen ")

    for seqs in seqs_input:
        if seqs:
            for seq in seqs:
                if seq:
                    num_of_seq+=1

    seq_by_mini_batch_size_input = seq_group_by_mini_batch_size(minibatch_unit, seqs_sorted_input, num_of_seq)
    seq_by_mini_batch_size_target = seq_group_by_mini_batch_size(minibatch_unit, seqs_sorted_target, num_of_seq)
    print("FINISHED : seq_group_by_mini_batch_size ")
    _1hot_input = seq_to_1hot(maxlen_input, seq_by_mini_batch_size_input, "input",minibatch_unit, num_of_seq)
    _1hot_target = seq_to_1hot(maxlen_target, seq_by_mini_batch_size_target, "target",minibatch_unit, num_of_seq)
    print("FINISHED : seq_to_1hot ")
    if minibatch_unit > total_sentence_num:
        minibatch_unit = total_sentence_num

    print("exit preprocessing")

    return _1hot_input, _1hot_target, maxlen_input, minibatch_unit, voca_dim, word_to_index_input, word_to_index_targrt, index_to_word_target

0 个答案:

没有答案