コード例 #1
0
def build_network(train_x, train_y, test_x, test_y, epochs, total, max_length):

    print("total words", total)
    model = tf.keras.Sequential([
        layers.Embedding(total + 1, 64, input_length=max_length),
        layers.Dropout(.1),
        layers.Flatten(),
        layers.Dense(600, activation='relu'),
        layers.Dense(300, activation='relu'),
        layers.Dense(16, activation='softmax')
    ])
    model.compile(
        optimizer='Adam',  # Optimizer
        # Loss function to minimize
        loss="sparse_categorical_crossentropy",
        metrics=['acc'])
    model.summary()
    print('# Fit model on training data')
    print('validation sets', test_x.shape, test_y.shape)
    # print('validation sets', test_x, test_y)
    print('train sets', train_x.shape, train_y.shape)
    history = model.fit(train_x,
                        train_y,
                        batch_size=2,
                        epochs=10,
                        validation_data=(test_x, test_y))
    print('\nhistory dict:', history.history)
    return model
コード例 #2
0
(input_train, y_train), (input_test, y_test) = imdb.load_data(num_words=max_features)
print(len(input_train), 'train sequences')
print(len(input_test), 'test sequences')

print('Pad sequences (samples x time)')
input_train = sequence.pad_sequences(input_train, maxlen=maxlen)
input_test = sequence.pad_sequences(input_test, maxlen=maxlen)
print('input_train shape:', input_train.shape)
print('input_test shape:', input_test.shape)


from tensorflow_core.python.keras import models
from tensorflow_core.python.keras import layers

network = models.Sequential()
network.add(layers.Embedding(max_features, 128))
network.add(layers.Conv1D(256,3,padding='valid',activation='relu',strides=1))
network.add(layers.MaxPooling1D())
network.add(layers.Bidirectional(layers.LSTM(128)))
network.add(layers.Dense(256, activation='relu'))
network.add(layers.Dense(64, activation='relu'))
network.add(layers.Dense(32, activation='relu'))
network.add(layers.Dense(16, activation='relu'))
network.add(layers.Dense(16, activation='relu'))
network.add(layers.Dense(1))
network.add(layers.Activation('sigmoid'))

#make the training data 80% and testing 20%
input_train = np.concatenate((input_train, input_test[:15000]))
input_test = input_test[15000:]
y_train = np.concatenate((y_train, y_test[:15000]))
コード例 #3
0
print('Pad sequences (samples x time)')
input_train = sequence.pad_sequences(input_train, maxlen=maxlen)
input_test = sequence.pad_sequences(input_test, maxlen=maxlen)
print('input_train shape:', input_train.shape)
print('input_test shape:', input_test.shape)
# make the training data 80% and testing 20%
x_train = np.concatenate((input_train, input_test[:15000]))
input_test = input_test[15000:]
y_train = np.concatenate((y_train, y_test[:15000]))
y_test = y_test[15000:]

from tensorflow_core.python.keras import models
from tensorflow_core.python.keras import layers

model = models.Sequential()
model.add(layers.Embedding(max_features, 32))
model.add(layers.SimpleRNN(32))
model.add(layers.Dense(1, activation='sigmoid'))

model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['acc'])

model.summary()
his = model.fit(x_train,
                y_train,
                epochs=3,
                batch_size=64,
                validation_split=0.025)

results = model.evaluate(input_test, y_test)
print(results)
コード例 #4
0
ファイル: np.py プロジェクト: jpegbert/TensorFlow2.0
print(o1)

x = tf.random.normal([4, 28 * 28])
# 创建全连接层,指定输出节点数和激活函数
fc = layers.Dense(512, activation=tf.nn.relu)
h1 = fc(x)  # 通过fc类完成一次全连接层的计算
print(vars(fc))

#%%
x = tf.random.normal([4, 4])
# 创建全连接层,指定输出节点数和激活函数
fc = layers.Dense(3, activation=tf.nn.relu)
h1 = fc(x)  # 通过fc类完成一次全连接层的计算
print(fc.non_trainable_variables)

embedding = layers.Embedding(10000, 100)

x = tf.ones([25000, 80])

embedding(x)

#%%
z = tf.random.normal([2, 10])  # 构造输出层的输出
y_onehot = tf.constant([1, 3])  # 构造真实值
y_onehot = tf.one_hot(y_onehot, depth=10)  # one-hot编码
# 输出层未使用Softmax函数,故from_logits设置为True
loss = keras.losses.categorical_crossentropy(y_onehot, z, from_logits=True)
loss = tf.reduce_mean(loss)  # 计算平均交叉熵损失
print(loss)

criteon = keras.losses.CategoricalCrossentropy(from_logits=True)
コード例 #5
0
input_train = sequence.pad_sequences(input_train, maxlen=maxlen)
input_test = sequence.pad_sequences(input_test, maxlen=maxlen)
print('input_train shape:', input_train.shape)
print('input_test shape:', input_test.shape)
# make the training data 80% and testing 20%
x_train = np.concatenate((input_train, input_test[:15000]))
input_test = input_test[15000:]
y_train = np.concatenate((y_train, y_test[:15000]))
y_test = y_test[15000:]

from tensorflow_core.python.keras import models
from tensorflow_core.python.keras import layers

embedding_size = 128
model = models.Sequential()
model.add(layers.Embedding(max_features, embedding_size, input_length=maxlen))
model.add(layers.Bidirectional(layers.LSTM(128, return_sequences=True)))
model.add(layers.Dropout(0.2))
model.add(layers.Bidirectional(layers.LSTM(128)))
model.add(layers.Dropout(0.2))
model.add(layers.Dense(256, activation='relu'))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(32, activation='relu'))
model.add(layers.Dense(16, activation='relu'))
model.add(layers.Dense(16, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))

model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['acc'])
his = model.fit(x_train,
                y_train,
                epochs=4,
コード例 #6
0
ファイル: single_LSTM.py プロジェクト: xmr1996/Project02
input_train = sequence.pad_sequences(input_train, maxlen=maxlen)
input_test = sequence.pad_sequences(input_test, maxlen=maxlen)
print('input_train shape:', input_train.shape)
print('input_test shape:', input_test.shape)
# make the training data 80% and testing 20%
x_train = np.concatenate((input_train, input_test[:15000]))
input_test = input_test[15000:]
y_train = np.concatenate((y_train, y_test[:15000]))
y_test = y_test[15000:]

from tensorflow_core.python.keras import models
from tensorflow_core.python.keras import layers

embedding_size = 128
model = models.Sequential()
model.add(layers.Embedding(max_features, embedding_size))
model.add(layers.Bidirectional(layers.LSTM(128)))
model.add(layers.Dropout(0.2))
model.add(layers.Dense(256, activation='relu'))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(32, activation='relu'))
model.add(layers.Dense(16, activation='relu'))
model.add(layers.Dense(16, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))

model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['acc'])

model.summary()
his = model.fit(x_train,
                y_train,
                epochs=3,