コード例 #1
0
    def __generate_seq(self, seed_int_encode, n_chars=250, diversity=0.2):
        '''
        Generate text using the current LSTM model
        
        Input:
            @seed_int_encode: Seed sequence encoded as integers
            @n_chars: Number of characters to generate
            @diversity: How randomized the character selection should be
        Output:
            @return: 'n_chars' characters of generated text
        '''

        # Begin with seed sequence
        int_encode = seed_int_encode

        # Translate seed text
        start_chars = [self.indices_char[x] for x in int_encode]
        message = ''.join(start_chars)

        # generate a fixed number of characters
        for _ in range(n_chars):

            # truncate sequences to a fixed length
            int_encode = pad_sequences([int_encode],
                                       maxlen=self.seq_len,
                                       truncating='pre')[0]

            # one hot encode
            hot_encode = to_categorical(int_encode,
                                        num_classes=self.vocab_size)

            # Change shape from: (seq_len, vocab)
            # to: (1, seq_len, vocab)
            # Since LSTM requires a tensor input
            hot_encode = np.expand_dims(hot_encode, 0)

            # Predict next character
            preds = self.model.predict(hot_encode, verbose=0)[0]
            yhat = self.__sample(preds, diversity)

            # Append int encoding to continue recurrant predictions
            int_encode = np.append(int_encode, yhat)

            # Keep track of full message generated
            message += self.indices_char[yhat]

        # Return generated message
        return (message)
コード例 #2
0
    def do_one_hot_encoding(self, just_labels):
        """
        This function is used to one hot encode the labels, i.e. convert them from categorical to binary encoded format

        :param just_labels: the label vector to be one hot encoded
        :return: a binary encoded label matrix
        """
        try:
            le = LabelEncoder()
            labels = le.fit_transform(just_labels)
            one_hot_encoded_labels = to_categorical(labels,
                                                    len(set(just_labels)))

            return one_hot_encoded_labels
        except Exception as e:
            self.logger.exception(e)
            sys.exit(1)
コード例 #3
0
                f.close()
                labels.append(label_id)

print('Found %s texts.' % len(texts))

# finally, vectorize the text samples into a 2D integer tensor
tokenizer = Tokenizer(num_words=MAX_NUM_WORDS)
tokenizer.fit_on_texts(texts)
sequences = tokenizer.texts_to_sequences(texts)

word_index = tokenizer.word_index
print('Found %s unique tokens.' % len(word_index))

data = pad_sequences(sequences, maxlen=MAX_SEQUENCE_LENGTH)

labels = to_categorical(np.asarray(labels))
print('Shape of data tensor:', data.shape)
print('Shape of label tensor:', labels.shape)

# split the data into a training set and a validation set
indices = np.arange(data.shape[0])
np.random.shuffle(indices)
data = data[indices]
labels = labels[indices]
num_validation_samples = int(VALIDATION_SPLIT * data.shape[0])

x_train = data[:-num_validation_samples]
y_train = labels[:-num_validation_samples]
x_val = data[-num_validation_samples:]
y_val = labels[-num_validation_samples:]
コード例 #4
0
    return model_fn_lib.ModelFnOps(mode=mode,
                                   predictions=predictions,
                                   loss=loss,
                                   train_op=train_op,
                                   eval_metric_ops=eval_metric_ops)


text_col = 'consumer_complaint_narrative'
target_col = 'product'

data_set = pd.read_csv('dataset/complaints.csv')

features = data_set[text_col].values
targets = data_set[target_col].values

targets = to_categorical(targets, 11)

tokenizer = Tokenizer(num_words=max_features)
tokenizer.fit_on_texts(features)

features_seq = tokenizer.texts_to_sequences(features)
word_index = tokenizer.word_index

X_train, X_test, y_train, y_test = train_test_split(features_seq,
                                                    targets,
                                                    random_state=55,
                                                    test_size=0.20)

X_train = sequence.pad_sequences(X_train, maxlen=maxlen, padding='post')
X_test = sequence.pad_sequences(X_test, maxlen=maxlen, padding='post')
コード例 #5
0
    output = lenet(myinput)
    # 建立模型
    model = Model(myinput, output)

    # 定义优化器,这里选用Adam优化器,学习率设置为0.0003
    adam = Adam(lr=0.0003)
    # 编译模型
    model.compile(optimizer=adam,
                  loss="categorical_crossentropy",
                  metrics=['accuracy'])

    # 准备数据
    # 获取输入的图像
    X = GetTrainDataByLabel('data')
    # 获取图像的label,这里使用to_categorical函数返回one-hot之后的label
    Y = to_categorical(GetTrainDataByLabel('labels'))

    # 开始训练模型,batch设置为200,一共50个epoch
    model.fit(X,
              Y,
              200,
              50,
              1,
              callbacks=[
                  TensorBoard('./LeNet/log', write_images=1, histogram_freq=1)
              ],
              validation_split=0.2,
              shuffle=True)
    # 保存模型
    model.save("lenet-no-activation-model.h5")
コード例 #6
0
    # 构建网络
    output = lenet(myinput)
    # 建立模型
    model = Model(myinput, output)

    # 定义优化器,这里选用Adam优化器,学习率设置为0.0003
    adam = Adam(lr=0.0003)
    # 编译模型
    model.compile(optimizer=adam,
                  loss="categorical_crossentropy",
                  metrics=['accuracy'])

    # 准备数据
    # 获取输入的图像
    X = get_train_data_by_label('data')
    # 获取图像的label,这里使用to_categorical函数返回one-hot之后的label
    Y = to_categorical(get_train_data_by_label('labels'))

    # 开始训练模型,batch设置为200,一共50个epoch
    model.fit(X,
              Y,
              200,
              50,
              1,
              callbacks=[
                  TensorBoard('./LeNet/log', write_images=1, histogram_freq=1)
              ],
              validation_split=0.2,
              shuffle=True)
    # 保存模型
    model.save("lenet-no-activation-model.h5")
コード例 #7
0
from tensorflow.contrib.keras.api.keras.datasets import mnist
from tensorflow.contrib.keras.api.keras.models import Sequential
from tensorflow.contrib.keras.api.keras.layers import Dense, Dropout, Activation
from tensorflow.contrib.keras.api.keras.optimizers import Adam
from tensorflow.contrib.keras.api.keras.utils import to_categorical
import tensorflow.contrib.lite as lite

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(60000, 784)
x_test = x_test.reshape(10000, 784)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)

model = Sequential()
model.add(Dense(512, input_shape=(784, )))
model.add(Activation("relu"))
model.add(Dropout(0.2))
model.add(Dense(512))
model.add(Activation("relu"))
model.add(Dropout(0.2))
model.add(Dense(10))
model.add(Activation("softmax"))
model.compile(loss="categorical_crossentropy",
              optimizer=Adam(),
              metrics=['accuracy'])
model.fit(x_train,
          y_train,