为二进制分类调整 tensorflow LSTM 代码

Posted

技术标签:

【中文标题】为二进制分类调整 tensorflow LSTM 代码【英文标题】:Adapting tensorflow LSTM code for binary classification 【发布时间】:2018-10-22 04:14:38 【问题描述】:

我正在尝试采用这个基本的 LSTM 模型 (https://github.com/suriyadeepan/rnn-from-scratch/blob/master/lstm.py),它是一个多对多序列模型,并将其转换为具有二进制结果的序列分类器。

我的结果和特征如下所示:

# Features: 
array([[62, 91, 57, ..., 91, 43, 87],
       [66, 20, 52, ..., 91, 33, 20],
       [66, 45, 52, ..., 70, 91, 66],
       ...,
       [72, 20, 20, ..., 17, 14, 66],
       [91, 25, 52, ..., 52, 14, 52],
       [72, 29, 66, ..., 21, 20, 52]], dtype=int32)

# Feature matrix shape
(118929, 20)


# Outcome 
array([[1],
       [0],
       [1],
       ...,
       [0],
       [1],
       [1]])

# Outcome shape
(118929, 1)

修改后的代码如下:

import tensorflow as tf
import numpy as np

import random
import argparse
import sys

from random import sample
import configparser
import os

import csv
import pickle as pkl

from sklearn.preprocessing import OneHotEncoder, LabelBinarizer, LabelEncoder
from sklearn.datasets import make_classification


def rand_batch_gen(x, y, batch_size):
    while True:
        sample_idx = sample(list(np.arange(len(x))), batch_size)
        yield x[sample_idx], y[sample_idx]



with open('data/paulg/metadata.pkl', 'rb') as f:
    metadata = pkl.load(f)
# read numpy arrays
X = np.load('data/paulg/idx_x.npy')
Y = np.load('data/paulg/idx_y.npy')
idx2w = metadata['idx2ch'] 
w2idx = metadata['ch2idx']


_, Y = make_classification(n_samples = 118929, n_classes = 2, n_features=2, n_redundant=0, n_informative=1, n_clusters_per_class=1)
label_encoder = LabelEncoder()
integer_encoded = label_encoder.fit_transform(Y)
Y = Y.reshape(-1,1)









BATCH_SIZE = 256

class LSTM_rnn():

    def __init__(self, state_size, num_classes,
            ckpt_path='ckpt/lstm1/',
            model_name='lstm1'):

        self.state_size = state_size
        self.num_classes = num_classes
        self.ckpt_path = ckpt_path
        self.model_name = model_name

        # build graph ops
        def __graph__():
            tf.reset_default_graph()
            # inputs
            xs_ = tf.placeholder(shape=[None, None], dtype=tf.int32)
            ys_ = tf.placeholder(shape=[None, 1], dtype=tf.int32)

            # embeddings
            embs = tf.get_variable('emb', [100, state_size])
            rnn_inputs = tf.nn.embedding_lookup(embs, xs_)

            # initial hidden state
            init_state = tf.placeholder(shape=[2, None, state_size], dtype=tf.float32, name='initial_state')
            # initializer
            xav_init = tf.contrib.layers.xavier_initializer
            # params
            W = tf.get_variable('W', shape=[4, self.state_size, self.state_size], initializer=xav_init())
            U = tf.get_variable('U', shape=[4, self.state_size, self.state_size], initializer=xav_init())
            #b = tf.get_variable('b', shape=[self.state_size], initializer=tf.constant_initializer(0.))

            # step - LSTM
            def step(prev, x):
                # gather previous internal state and output state
                st_1, ct_1 = tf.unstack(prev)

                # GATES
                #
                #  input gate
                i = tf.sigmoid(tf.matmul(x,U[0]) + tf.matmul(st_1,W[0]))
                #  forget gate
                f = tf.sigmoid(tf.matmul(x,U[1]) + tf.matmul(st_1,W[1]))
                #  output gate
                o = tf.sigmoid(tf.matmul(x,U[2]) + tf.matmul(st_1,W[2]))
                #  gate weights
                g = tf.tanh(tf.matmul(x,U[3]) + tf.matmul(st_1,W[3]))

                # new internal cell state
                ct = ct_1*f + g*i
                # output state
                st = tf.tanh(ct)*o
                return tf.stack([st, ct])



            states = tf.scan(step, 
                    tf.transpose(rnn_inputs, [1,0,2]),
                    initializer=init_state)

            # predictions
            V = tf.get_variable('V', shape=[state_size, num_classes], 
                                initializer=xav_init())
            bo = tf.get_variable('bo', shape=[num_classes], 
                                 initializer=tf.constant_initializer(0.))


            # get last state before reshape/transpose
            last_state = states[-1]


            # transpose
            states = tf.transpose(states, [1,2,0,3])[0]

            states_reshaped = tf.reshape(states, [-1, state_size])
            logits = tf.matmul(states_reshaped, V) + bo

    # predictions
            predictions = tf.nn.softmax(logits) 

            # optimization
            losses = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=ys_)
            loss = tf.reduce_mean(losses)
            train_op = tf.train.AdagradOptimizer(learning_rate=0.1).minimize(loss)

            # expose symbols
            self.xs_ = xs_
            self.ys_ = ys_
            self.loss = loss
            self.train_op = train_op
            self.predictions = predictions
            self.last_state = last_state
            self.init_state = init_state

        # build graph
        __graph__()


    ####
    # training
    def train(self, train_set, epochs=100):
        # training session
        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            train_loss = 0
            try:
                for i in range(epochs):
                    for j in range(100):
                        xs, ys = train_set.__next__()
                        batch_size = xs.shape[0]
                        _, train_loss_ = sess.run([self.train_op, self.loss], feed_dict = 
                                self.xs_ : xs,
                                self.ys_ : ys.flatten(),
                                self.init_state : np.zeros([2, batch_size, self.state_size])
                            )
                        train_loss += train_loss_
                    print('[] loss : '.format(i,train_loss/100))
                    train_loss = 0
            except KeyboardInterrupt:
                print('interrupted by user at ' + str(i))

            # training ends here; 
            #  save checkpoint
            saver = tf.train.Saver()
            saver.save(sess, self.ckpt_path + self.model_name, global_step=i)







#### main function
if __name__ == '__main__':

    # create the model
    model = LSTM_rnn(state_size = 512, num_classes=1)

    # get train set
    train_set = rand_batch_gen(X, Y ,batch_size=BATCH_SIZE)

    # start training
    model.train(train_set)

我收到错误消息: “排名不匹配:标签排名(收到 2)应该等于 logits 排名减去 1(收到 2)。”

你知道我怎样才能成功地将这段代码用于二进制分类吗?

【问题讨论】:

【参考方案1】:

我不确定您是否还有其他错误。此错误来自sparse_softmax_cross_entropy_with_logits。在您的情况下,您的标签应该是一个长度为 118929 的向量,logit 应该是一个形状为 (118929, 2) 的矩阵。不要从make_classificationY = Y.reshape(-1,1))重塑你的Y

【讨论】:

嗯,由于某种原因,我仍然收到该错误消息。 @Slyron 这个错误真正指的是哪一行?如果是sparse_softmax_cross_entropy_with_logits,能否测试并显示logitsys_的形状?【参考方案2】:

将形状更改为 [无] 可能会有所帮助。 ys_ = tf.placeholder(shape=[None], dtype=tf.int32)

【讨论】:

以上是关于为二进制分类调整 tensorflow LSTM 代码的主要内容,如果未能解决你的问题,请参考以下文章

tensorflow实现基于LSTM的文本分类方法

为 LSTM 二进制分类构建语音数据集

tensorflow实现基于LSTM的文本分类方法

在 TensorFlow 中使用多对多 LSTM 进行视频分类

芝麻HTTP:TensorFlow LSTM MNIST分类

如何在 MultiOutput LSTM Tensorflow 中优先考虑某些输出?