• 大小: 11.95MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-08-06
  • 语言: Python
  • 标签:

资源简介

我是“小诗姬”,全唐诗作为训练数据。可以写押韵自由诗、藏头诗、给定若干字作为主题的诗

资源截图

代码片段和文件信息

import logging
import time
from enum import Enum
import heapq
import numpy as np
import tensorflow as tf
from rhyme_helper import RhymeWords

logging.getLogger(‘tensorflow‘).setLevel(logging.WARNING)
SampleType = Enum(‘SampleType‘(‘max_prob‘ ‘weighted_sample‘ ‘rhyme‘‘select_given‘))

class CharRNNLM(object):
    def __init__(self is_training batch_size num_unrollings vocab_sizew2v_model
                 hidden_size max_grad_norm embedding_size num_layers
                 learning_rate cell_type dropout=0.0 input_dropout=0.0 infer=False):
        self.batch_size = batch_size
        self.num_unrollings = num_unrollings
        if infer:
            self.batch_size = 1
            self.num_unrollings = 1
        self.hidden_size = hidden_size
        self.vocab_size = vocab_size
        self.max_grad_norm = max_grad_norm
        self.num_layers = num_layers
        self.embedding_size = embedding_size
        self.cell_type = cell_type
        self.dropout = dropout
        self.input_dropout = input_dropout
        self.w2v_model = w2v_model

        if embedding_size <= 0:
            self.input_size = vocab_size
            self.input_dropout = 0.0
        else:
            self.input_size = embedding_size

        self.input_data = tf.placeholder(tf.int64 [self.batch_size self.num_unrollings] name=‘inputs‘)
        self.targets =  tf.placeholder(tf.int64 [self.batch_size self.num_unrollings] name=‘targets‘)

        if self.cell_type == ‘rnn‘:
            cell_fn = tf.nn.rnn_cell.BasicRNNCell
        elif self.cell_type == ‘lstm‘:
            cell_fn = tf.nn.rnn_cell.BasicLSTMCell
        elif self.cell_type == ‘gru‘:
            cell_fn = tf.nn.rnn_cell.GRUCell

        params = dict()
        #params = {‘input_size‘: self.input_size}
        if self.cell_type == ‘lstm‘:
            params[‘forget_bias‘] = 1.0  # 1.0 is default value
        cell = cell_fn(self.hidden_size **params)

        cells = [cell]
        #params[‘input_size‘] = self.hidden_size
        for i in range(self.num_layers-1):
            higher_layer_cell = cell_fn(self.hidden_size **params)
            cells.append(higher_layer_cell)

        if is_training and self.dropout > 0:
            cells = [tf.nn.rnn_cell.DropoutWrapper(cell output_keep_prob=1.0-self.dropout) for cell in cells]

        multi_cell = tf.nn.rnn_cell.MultiRNNCell(cells)

        with tf.name_scope(‘initial_state‘):
            self.zero_state = multi_cell.zero_state(self.batch_size tf.float32)
            if self.cell_type == ‘rnn‘ or self.cell_type == ‘gru‘:
                self.initial_state = tuple(
                        [tf.placeholder(tf.float32
                            [self.batch_size multi_cell.state_size[idx]]
                            ‘initial_state_‘+str(idx+1)) for idx in range(self.num_layers)])
            elif self.cell_type == ‘lstm‘:
                self.initial_state = tuple(
                        [tf.nn.rnn_cell.LSTMStateTuple(
             

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-08-25 06:01  poet-master\
     文件         666  2017-08-25 06:01  poet-master\README.md
     文件       12877  2017-08-25 06:01  poet-master\char_rnn_model.py
     文件        6216  2017-08-25 06:01  poet-master\config_poem.py
     目录           0  2017-08-25 06:01  poet-master\data\
     目录           0  2017-08-25 06:01  poet-master\data\poem\
     文件     8399779  2017-08-25 06:01  poet-master\data\poem\poem_ids.txt
     文件     9458157  2017-08-25 06:01  poet-master\data\poem\poems_edge_split.txt
     文件       11310  2017-08-25 06:01  poet-master\data\poem\rhyme_words.txt
     文件     2832643  2017-08-25 06:01  poet-master\data\poem\vectors_poem.bin
     文件        9342  2017-08-25 06:01  poet-master\data_loader.py
     目录           0  2017-08-25 06:01  poet-master\doc\
     文件       48580  2017-08-25 06:01  poet-master\doc\client.png
     文件       55386  2017-08-25 06:01  poet-master\doc\train.png
     文件        1104  2017-08-25 06:01  poet-master\poem_server.py
     文件         875  2017-08-25 06:01  poet-master\rhyme_helper.py
     文件        8515  2017-08-25 06:01  poet-master\train.py
     文件        1282  2017-08-25 06:01  poet-master\word2vec_helper.py
     文件        6342  2017-08-25 06:01  poet-master\write_poem.py

评论

共有 条评论