本文整理汇总了Python中tflearn.input_data函数的典型用法代码示例。如果您正苦于以下问题:Python input_data函数的具体用法?Python input_data怎么用?Python input_data使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了input_data函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: build_nce_model
def build_nce_model(num_words, num_docs, doc_embedding_size=doc_embedding_size, word_embedding_size=word_embedding_size):
X1 = tflearn.input_data(shape=[None, 1])
X2 = tflearn.input_data(shape=[None, 3])
Y = tf.placeholder(tf.float32, [None, 1])
d1, = tflearn.embedding(X1, input_dim=num_docs, output_dim=doc_embedding_size)
w1, w2, w3 = tflearn.embedding(X2, input_dim=num_words, output_dim=word_embedding_size)
embedding_layer = tflearn.merge([d1, w1, w2, w3], mode='concat')
num_classes = num_words
dim = doc_embedding_size + 3*word_embedding_size
with tf.variable_scope("NCELoss"):
weights = tflearn.variables.variable('W', [num_classes, dim])
biases = tflearn.variables.variable('b', [num_classes])
batch_loss = tf.nn.nce_loss(weights, biases, embedding_layer, Y, num_sampled=100, num_classes=num_classes)
loss = tf.reduce_mean(batch_loss)
optimizer = tf.train.AdamOptimizer(learning_rate=0.001)
trainop = tflearn.TrainOp(loss=loss, optimizer=optimizer,
metric=None, batch_size=32)
trainer = tflearn.Trainer(train_ops=trainop, tensorboard_verbose=0, checkpoint_path='embedding_model_nce')
return trainer, X1, X2, Y
示例2: test_dnn
def test_dnn(self):
with tf.Graph().as_default():
X = [3.3,4.4,5.5,6.71,6.93,4.168,9.779,6.182,7.59,2.167,7.042,10.791,5.313,7.997,5.654,9.27,3.1]
Y = [1.7,2.76,2.09,3.19,1.694,1.573,3.366,2.596,2.53,1.221,2.827,3.465,1.65,2.904,2.42,2.94,1.3]
input = tflearn.input_data(shape=[None])
linear = tflearn.single_unit(input)
regression = tflearn.regression(linear, optimizer='sgd', loss='mean_square',
metric='R2', learning_rate=0.01)
m = tflearn.DNN(regression)
# Testing fit and predict
m.fit(X, Y, n_epoch=1000, show_metric=True, snapshot_epoch=False)
res = m.predict([3.2])[0]
self.assertGreater(res, 1.3, "DNN test (linear regression) failed! with score: " + str(res) + " expected > 1.3")
self.assertLess(res, 1.8, "DNN test (linear regression) failed! with score: " + str(res) + " expected < 1.8")
# Testing save method
m.save("test_dnn.tflearn")
self.assertTrue(os.path.exists("test_dnn.tflearn"))
with tf.Graph().as_default():
input = tflearn.input_data(shape=[None])
linear = tflearn.single_unit(input)
regression = tflearn.regression(linear, optimizer='sgd', loss='mean_square',
metric='R2', learning_rate=0.01)
m = tflearn.DNN(regression)
# Testing load method
m.load("test_dnn.tflearn")
res = m.predict([3.2])[0]
self.assertGreater(res, 1.3, "DNN test (linear regression) failed after loading model! score: " + str(res) + " expected > 1.3")
self.assertLess(res, 1.8, "DNN test (linear regression) failed after loading model! score: " + str(res) + " expected < 1.8")
示例3: test_conv_layers
def test_conv_layers(self):
X = [[0., 0., 0., 0.], [1., 1., 1., 1.], [0., 0., 1., 0.], [1., 1., 1., 0.]]
Y = [[1., 0.], [0., 1.], [1., 0.], [0., 1.]]
with tf.Graph().as_default():
g = tflearn.input_data(shape=[None, 4])
g = tflearn.reshape(g, new_shape=[-1, 2, 2, 1])
g = tflearn.conv_2d(g, 4, 2, activation='relu')
g = tflearn.max_pool_2d(g, 2)
g = tflearn.fully_connected(g, 2, activation='softmax')
g = tflearn.regression(g, optimizer='sgd', learning_rate=1.)
m = tflearn.DNN(g)
m.fit(X, Y, n_epoch=100, snapshot_epoch=False)
# TODO: Fix test
#self.assertGreater(m.predict([[1., 0., 0., 0.]])[0][0], 0.5)
# Bulk Tests
with tf.Graph().as_default():
g = tflearn.input_data(shape=[None, 4])
g = tflearn.reshape(g, new_shape=[-1, 2, 2, 1])
g = tflearn.conv_2d(g, 4, 2)
g = tflearn.conv_2d(g, 4, 1)
g = tflearn.conv_2d_transpose(g, 4, 2, [2, 2])
g = tflearn.max_pool_2d(g, 2)
示例4: xor_operation
def xor_operation():
# Function to simulate XOR operation using graph combo of NAND and OR
X = [[0., 0.], [0., 1.], [1., 0.], [1., 1.]]
Y_nand = [[1.], [1.], [1.], [0.]]
Y_or = [[0.], [1.], [1.], [1.]]
with tf.Graph().as_default():
graph = tflearn.input_data(shape=[None, 2])
graph_nand = tflearn.fully_connected(graph, 32, activation='linear')
graph_nand = tflearn.fully_connected(graph_nand, 32, activation='linear')
graph_nand = tflearn.fully_connected(graph_nand, 1, activation='sigmoid')
graph_nand = tflearn.regression(graph_nand, optimizer='sgd', learning_rate=2., loss='binary_crossentropy')
graph_or = tflearn.fully_connected(graph, 32, activation='linear')
graph_or = tflearn.fully_connected(graph_or, 32, activation='linear')
graph_or = tflearn.fully_connected(graph_or, 1, activation='sigmoid')
graph_or = tflearn.regression(graph_or, optimizer='sgd', learning_rate=2., loss='binary_crossentropy')
graph_xor = tflearn.merge([graph_nand, graph_or], mode='elemwise_mul')
# Model training
model = tflearn.DNN(graph_xor)
model.fit(X, [Y_nand, Y_or], n_epoch=100, snapshot_epoch=False)
prediction = model.predict([[0., 1.]])
print("Prediction: ", prediction)
示例5: yn_net
def yn_net():
net = tflearn.input_data(shape=[None, img_rows, img_cols, 1]) #D = 256, 256
net = tflearn.conv_2d(net,nb_filter=8,filter_size=3, activation='relu', name='conv0.1')
net = tflearn.conv_2d(net,nb_filter=8,filter_size=3, activation='relu', name='conv0.2')
net = tflearn.max_pool_2d(net, kernel_size = [2,2], name='maxpool0') #D = 128, 128
net = tflearn.dropout(net,0.75,name='dropout0')
net = tflearn.conv_2d(net,nb_filter=16,filter_size=3, activation='relu', name='conv1.1')
net = tflearn.conv_2d(net,nb_filter=16,filter_size=3, activation='relu', name='conv1.2')
net = tflearn.max_pool_2d(net, kernel_size = [2,2], name='maxpool1') #D = 64, 64
net = tflearn.dropout(net,0.75,name='dropout0')
net = tflearn.conv_2d(net,nb_filter=32,filter_size=3, activation='relu', name='conv2.1')
net = tflearn.conv_2d(net,nb_filter=32,filter_size=3, activation='relu', name='conv2.2')
net = tflearn.max_pool_2d(net, kernel_size = [2,2], name='maxpool2') #D = 32 by 32
net = tflearn.dropout(net,0.75,name='dropout0')
net = tflearn.conv_2d(net,nb_filter=32,filter_size=3, activation='relu', name='conv3.1')
net = tflearn.conv_2d(net,nb_filter=32,filter_size=3, activation='relu', name='conv3.2')
net = tflearn.max_pool_2d(net, kernel_size = [2,2], name='maxpool3') #D = 16 by 16
net = tflearn.dropout(net,0.75,name='dropout0')
# net = tflearn.conv_2d(net,nb_filter=64,filter_size=3, activation='relu', name='conv4.1')
# net = tflearn.conv_2d(net,nb_filter=64,filter_size=3, activation='relu', name='conv4.2')
# net = tflearn.max_pool_2d(net, kernel_size = [2,2], name='maxpool4') #D = 8 by 8
# net = tflearn.dropout(net,0.75,name='dropout0')
net = tflearn.fully_connected(net, n_units = 128, activation='relu', name='fc1')
net = tflearn.fully_connected(net, 2, activation='sigmoid')
net = tflearn.regression(net, optimizer='adam', learning_rate=0.001)
model = tflearn.DNN(net, tensorboard_verbose=1,tensorboard_dir='/tmp/tflearn_logs/')
return model
示例6: __init__
def __init__(self, s_date, n_frame):
self.n_epoch = 20
prev_bd = int(s_date[:6])-1
prev_ed = int(s_date[9:15])-1
if prev_bd%100 == 0: prev_bd -= 98
if prev_ed%100 == 0: prev_ed -= 98
pred_s_date = "%d01_%d01" % (prev_bd, prev_ed)
prev_model = '../model/tflearn/reg_l3_bn/big/%s' % pred_s_date
self.model_dir = '../model/tflearn/reg_l3_bn/big/%s' % s_date
tf.reset_default_graph()
tflearn.init_graph(gpu_memory_fraction=0.1)
input_layer = tflearn.input_data(shape=[None, 23*n_frame], name='input')
dense1 = tflearn.fully_connected(input_layer, 400, name='dense1', activation='relu')
dense1n = tflearn.batch_normalization(dense1, name='BN1')
dense2 = tflearn.fully_connected(dense1n, 100, name='dense2', activation='relu')
dense2n = tflearn.batch_normalization(dense2, name='BN2')
dense3 = tflearn.fully_connected(dense2n, 1, name='dense3')
output = tflearn.single_unit(dense3)
regression = tflearn.regression(output, optimizer='adam', loss='mean_square',
metric='R2', learning_rate=0.001)
self.estimators = tflearn.DNN(regression)
if os.path.exists('%s/model.tfl' % prev_model):
self.estimators.load('%s/model.tfl' % prev_model)
self.n_epoch = 10
if not os.path.exists(self.model_dir):
os.makedirs(self.model_dir)
示例7: use_tflearn
def use_tflearn():
import tflearn
# Data loading and preprocessing
import tflearn.datasets.mnist as mnist
X, Y, testX, testY = mnist.load_data(one_hot=True)
# Building deep neural network
input_layer = tflearn.input_data(shape=[None, 784])
dense1 = tflearn.fully_connected(input_layer, 64, activation='tanh',
regularizer='L2', weight_decay=0.001)
dropout1 = tflearn.dropout(dense1, 0.8)
dense2 = tflearn.fully_connected(dropout1, 64, activation='tanh',
regularizer='L2', weight_decay=0.001)
dropout2 = tflearn.dropout(dense2, 0.8)
softmax = tflearn.fully_connected(dropout2, 10, activation='softmax')
# Regression using SGD with learning rate decay and Top-3 accuracy
sgd = tflearn.SGD(learning_rate=0.1, lr_decay=0.96, decay_step=1000)
top_k = tflearn.metrics.Top_k(3)
net = tflearn.regression(softmax, optimizer=sgd, metric=top_k,
loss='categorical_crossentropy')
# Training
model = tflearn.DNN(net, tensorboard_verbose=0)
model.fit(X, Y, n_epoch=20, validation_set=(testX, testY),
show_metric=True, run_id="dense_model")
示例8: do_rnn
def do_rnn(trainX, testX, trainY, testY):
global n_words
# Data preprocessing
# Sequence padding
print "GET n_words embedding %d" % n_words
trainX = pad_sequences(trainX, maxlen=MAX_DOCUMENT_LENGTH, value=0.)
testX = pad_sequences(testX, maxlen=MAX_DOCUMENT_LENGTH, value=0.)
# Converting labels to binary vectors
trainY = to_categorical(trainY, nb_classes=2)
testY = to_categorical(testY, nb_classes=2)
# Network building
net = tflearn.input_data([None, MAX_DOCUMENT_LENGTH])
net = tflearn.embedding(net, input_dim=n_words, output_dim=128)
net = tflearn.lstm(net, 128, dropout=0.8)
net = tflearn.fully_connected(net, 2, activation='softmax')
net = tflearn.regression(net, optimizer='adam', learning_rate=0.001,
loss='categorical_crossentropy')
# Training
model = tflearn.DNN(net, tensorboard_verbose=3)
model.fit(trainX, trainY, validation_set=(testX, testY), show_metric=True,
batch_size=32,run_id="maidou")
示例9: generate_network
def generate_network(self):
""" Return tflearn cnn network.
"""
print(self.image_size, self.n_epoch, self.batch_size)
if not isinstance(self.image_size, list) \
or not isinstance(self.n_epoch, int) \
or not isinstance(self.batch_size, int):
raise ValueError("Insufficient values to generate network.\n"
"Need (n_epoch, int), (batch_size, int),"
"(image_size, list)")
network = tflearn.input_data(
shape=[None, self.image_size[0], self.image_size[1],
self.IMAGE_CHANNEL_NUM],
data_preprocessing=self.generate_image_preprocessing(),
data_augmentation=self.generate_image_augumentation())
dnn_network = DnnNetwork()
if self.network_type == NetworkType.cnn.name:
network = dnn_network.build_cnn_network(network)
elif self.network_type == NetworkType.resnet.name:
network = dnn_network.build_residual_network(network)
elif self.network_type == NetworkType.alex.name:
network = dnn_network.build_alex_network(network)
elif self.network_type == NetworkType.vgg.name:
network = dnn_network.build_vgg_network(network)
elif self.network_type == NetworkType.net_in_net.name:
network = dnn_network.build_network_in_network(network)
elif self.network_type == NetworkType.lenet.name:
network = dnn_network.build_le_network(network)
else:
raise NameError("invalid network_type: {}".format(self.network_type))
return network
示例10: deep_model
def deep_model(self, wide_inputs, n_inputs, n_nodes=[100, 50], use_dropout=False):
'''
Model - deep, i.e. two-layer fully connected network model
'''
cc_input_var = {}
cc_embed_var = {}
flat_vars = []
if self.verbose:
print ("--> deep model: %s categories, %d continuous" % (len(self.categorical_columns), n_inputs))
for cc, cc_size in self.categorical_columns.items():
cc_input_var[cc] = tflearn.input_data(shape=[None, 1], name="%s_in" % cc, dtype=tf.int32)
# embedding layers only work on CPU! No GPU implementation in tensorflow, yet!
cc_embed_var[cc] = tflearn.layers.embedding_ops.embedding(cc_input_var[cc], cc_size, 8, name="deep_%s_embed" % cc)
if self.verbose:
print (" %s_embed = %s" % (cc, cc_embed_var[cc]))
flat_vars.append(tf.squeeze(cc_embed_var[cc], squeeze_dims=[1], name="%s_squeeze" % cc))
network = tf.concat(1, [wide_inputs] + flat_vars, name="deep_concat")
for k in range(len(n_nodes)):
network = tflearn.fully_connected(network, n_nodes[k], activation="relu", name="deep_fc%d" % (k+1))
if use_dropout:
network = tflearn.dropout(network, 0.5, name="deep_dropout%d" % (k+1))
if self.verbose:
print ("Deep model network before output %s" % network)
network = tflearn.fully_connected(network, 1, activation="linear", name="deep_fc_output", bias=False)
network = tf.reshape(network, [-1, 1]) # so that accuracy is binary_accuracy
if self.verbose:
print ("Deep model network %s" % network)
return network
示例11: do_rnn
def do_rnn(x,y):
global max_document_length
print "RNN"
trainX, testX, trainY, testY = train_test_split(x, y, test_size=0.4, random_state=0)
y_test=testY
trainX = pad_sequences(trainX, maxlen=max_document_length, value=0.)
testX = pad_sequences(testX, maxlen=max_document_length, value=0.)
# Converting labels to binary vectors
trainY = to_categorical(trainY, nb_classes=2)
testY = to_categorical(testY, nb_classes=2)
# Network building
net = tflearn.input_data([None, max_document_length])
net = tflearn.embedding(net, input_dim=10240000, output_dim=128)
net = tflearn.lstm(net, 128, dropout=0.8)
net = tflearn.fully_connected(net, 2, activation='softmax')
net = tflearn.regression(net, optimizer='adam', learning_rate=0.001,
loss='categorical_crossentropy')
# Training
model = tflearn.DNN(net, tensorboard_verbose=0)
model.fit(trainX, trainY, validation_set=0.1, show_metric=True,
batch_size=10,run_id="webshell",n_epoch=5)
y_predict_list=model.predict(testX)
y_predict=[]
for i in y_predict_list:
if i[0] > 0.5:
y_predict.append(0)
else:
y_predict.append(1)
do_metrics(y_test, y_predict)
示例12: test_sequencegenerator
def test_sequencegenerator(self):
with tf.Graph().as_default():
text = "123456789101234567891012345678910123456789101234567891012345678910"
maxlen = 5
X, Y, char_idx = \
tflearn.data_utils.string_to_semi_redundant_sequences(text, seq_maxlen=maxlen, redun_step=3)
g = tflearn.input_data(shape=[None, maxlen, len(char_idx)])
g = tflearn.lstm(g, 32)
g = tflearn.dropout(g, 0.5)
g = tflearn.fully_connected(g, len(char_idx), activation='softmax')
g = tflearn.regression(g, optimizer='adam', loss='categorical_crossentropy',
learning_rate=0.1)
m = tflearn.SequenceGenerator(g, dictionary=char_idx,
seq_maxlen=maxlen,
clip_gradients=5.0)
m.fit(X, Y, validation_set=0.1, n_epoch=100, snapshot_epoch=False)
res = m.generate(10, temperature=1., seq_seed="12345")
self.assertEqual(res, "123456789101234", "SequenceGenerator test failed! Generated sequence: " + res + " expected '123456789101234'")
# Testing save method
m.save("test_seqgen.tflearn")
self.assertTrue(os.path.exists("test_seqgen.tflearn"))
# Testing load method
m.load("test_seqgen.tflearn")
res = m.generate(10, temperature=1., seq_seed="12345")
self.assertEqual(res, "123456789101234", "SequenceGenerator test failed after loading model! Generated sequence: " + res + " expected '123456789101234'")
示例13: test_regression_placeholder
def test_regression_placeholder(self):
'''
Check that regression does not duplicate placeholders
'''
with tf.Graph().as_default():
g = tflearn.input_data(shape=[None, 2])
g_nand = tflearn.fully_connected(g, 1, activation='linear')
with tf.name_scope("Y"):
Y_in = tf.placeholder(shape=[None, 1], dtype=tf.float32, name="Y")
tflearn.regression(g_nand, optimizer='sgd',
placeholder=Y_in,
learning_rate=2.,
loss='binary_crossentropy',
op_name="regression1",
name="Y")
# for this test, just use the same default trainable_vars
# in practice, this should be different for the two regressions
tflearn.regression(g_nand, optimizer='adam',
placeholder=Y_in,
learning_rate=2.,
loss='binary_crossentropy',
op_name="regression2",
name="Y")
self.assertEqual(len(tf.get_collection(tf.GraphKeys.TARGETS)), 1)
示例14: model_for_type
def model_for_type(neural_net_type, tile_size, on_band_count):
"""The neural_net_type can be: one_layer_relu,
one_layer_relu_conv,
two_layer_relu_conv."""
network = tflearn.input_data(shape=[None, tile_size, tile_size, on_band_count])
# NN architectures mirror ch. 3 of www.cs.toronto.edu/~vmnih/docs/Mnih_Volodymyr_PhD_Thesis.pdf
if neural_net_type == "one_layer_relu":
network = tflearn.fully_connected(network, 64, activation="relu")
elif neural_net_type == "one_layer_relu_conv":
network = conv_2d(network, 64, 12, strides=4, activation="relu")
network = max_pool_2d(network, 3)
elif neural_net_type == "two_layer_relu_conv":
network = conv_2d(network, 64, 12, strides=4, activation="relu")
network = max_pool_2d(network, 3)
network = conv_2d(network, 128, 4, activation="relu")
else:
print("ERROR: exiting, unknown layer type for neural net")
# classify as road or not road
softmax = tflearn.fully_connected(network, 2, activation="softmax")
# hyperparameters based on www.cs.toronto.edu/~vmnih/docs/Mnih_Volodymyr_PhD_Thesis.pdf
momentum = tflearn.optimizers.Momentum(learning_rate=0.005, momentum=0.9, lr_decay=0.0002, name="Momentum")
net = tflearn.regression(softmax, optimizer=momentum, loss="categorical_crossentropy")
return tflearn.DNN(net, tensorboard_verbose=0)
示例15: main
def main():
load_vectors("./vectors.bin")
init_seq()
xlist = []
ylist = []
test_X = None
#for i in range(len(seq)-100):
for i in range(1000):
sequence = seq[i:i+20]
xlist.append(sequence)
ylist.append(seq[i+20])
if test_X is None:
test_X = np.array(sequence)
(match_word, max_cos) = vector2word(seq[i+20])
print "right answer=", match_word, max_cos
X = np.array(xlist)
Y = np.array(ylist)
net = tflearn.input_data([None, 20, 200])
net = tflearn.lstm(net, 200)
net = tflearn.fully_connected(net, 200, activation='linear')
net = tflearn.regression(net, optimizer='sgd', learning_rate=0.1,
loss='mean_square')
model = tflearn.DNN(net)
model.fit(X, Y, n_epoch=1000, batch_size=1,snapshot_epoch=False,show_metric=True)
model.save("model")
predict = model.predict([test_X])
#print predict
#for v in test_X:
# print vector2word(v)
(match_word, max_cos) = vector2word(predict[0])
print "predict=", match_word, max_cos