• 大小: 437KB
    文件类型: .zip
    金币: 2
    下载: 1 次
    发布日期: 2021-10-31
  • 语言: 其他
  • 标签: nlp  

资源简介

git上的资源,可以用于自动创作诗,歌,小说等,数据集的话根据自己的需求去找,网上很多的

资源截图

代码片段和文件信息

import tensorflow as tf
from tensorflow.contrib import rnn
from tensorflow.contrib import legacy_seq2seq

import numpy as np


class Model():
    def __init__(self args training=True):
        self.args = args
        if not training:
            args.batch_size = 1
            args.seq_length = 1

        # choose different rnn cell 
        if args.model == ‘rnn‘:
            cell_fn = rnn.RNNCell
        elif args.model == ‘gru‘:
            cell_fn = rnn.GRUCell
        elif args.model == ‘lstm‘:
            cell_fn = rnn.LSTMCell
        elif args.model == ‘nas‘:
            cell_fn = rnn.NASCell
        else:
            raise Exception(“model type not supported: {}“.format(args.model))

        # warp multi layered rnn cell into one cell with dropout
        cells = []
        for _ in range(args.num_layers):
            cell = cell_fn(args.rnn_size)
            if training and (args.output_keep_prob < 1.0 or args.input_keep_prob < 1.0):
                cell = rnn.DropoutWrapper(cell
                                          input_keep_prob=args.input_keep_prob
                                          output_keep_prob=args.output_keep_prob)
            cells.append(cell)
        self.cell = cell = rnn.MultiRNNCell(cells state_is_tuple=True)

        # input/target data (int32 since input is char-level)
        self.input_data = tf.placeholder(
            tf.int32 [args.batch_size args.seq_length])
        self.targets = tf.placeholder(
            tf.int32 [args.batch_size args.seq_length])
        self.initial_state = cell.zero_state(args.batch_size tf.float32)

        # softmax output layer use softmax to classify
        with tf.variable_scope(‘rnnlm‘):
            softmax_w = tf.get_variable(“softmax_w“
                                        [args.rnn_size args.vocab_size])
            softmax_b = tf.get_variable(“softmax_b“ [args.vocab_size])

        # transform input to embedding
        embedding = tf.get_variable(“embedding“ [args.vocab_size args.rnn_size])
        inputs = tf.nn.embedding_lookup(embedding self.input_data)

        # dropout beta testing: double check which one should affect next line
        if training and args.output_keep_prob:
            inputs = tf.nn.dropout(inputs args.output_keep_prob)

        # unstack the input to fits in rnn model
        inputs = tf.split(inputs args.seq_length 1)
        inputs = [tf.squeeze(input_ [1]) for input_ in inputs]

        # loop function for rnn_decoder which take the previous i-th cell‘s output and generate the (i+1)-th cell‘s input
        def loop(prev _):
            prev = tf.matmul(prev softmax_w) + softmax_b
            prev_symbol = tf.stop_gradient(tf.argmax(prev 1))
            return tf.nn.embedding_lookup(embedding prev_symbol)

        # rnn_decoder to generate the ouputs and final state. When we are not training the model we use the loop function.
        outputs last_state = legacy_seq2seq.rnn_decoder(inputs self.initial_st

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-05-11 22:55  char-rnn-tensorflow-master\
     文件        1113  2018-05-11 22:55  char-rnn-tensorflow-master\.gitignore
     文件        1377  2018-05-11 22:55  char-rnn-tensorflow-master\.travis.yml
     文件        1080  2018-05-11 22:55  char-rnn-tensorflow-master\LICENSE.md
     文件        3622  2018-05-11 22:55  char-rnn-tensorflow-master\README.md
     目录           0  2018-05-11 22:55  char-rnn-tensorflow-master\data\
     目录           0  2018-05-11 22:55  char-rnn-tensorflow-master\data\tinyshakespeare\
     文件     1115394  2018-05-11 22:55  char-rnn-tensorflow-master\data\tinyshakespeare\input.txt
     目录           0  2018-05-11 22:55  char-rnn-tensorflow-master\logs\
     文件          71  2018-05-11 22:55  char-rnn-tensorflow-master\logs\.gitignore
     文件        5639  2018-05-11 22:55  char-rnn-tensorflow-master\model.py
     文件        1732  2018-05-11 22:55  char-rnn-tensorflow-master\sample.py
     目录           0  2018-05-11 22:55  char-rnn-tensorflow-master\save\
     文件          71  2018-05-11 22:55  char-rnn-tensorflow-master\save\.gitignore
     文件        7542  2018-05-11 22:55  char-rnn-tensorflow-master\train.py
     文件        3321  2018-05-11 22:55  char-rnn-tensorflow-master\utils.py

评论

共有 条评论