在Tensorflow(tflearn)问题中具有完全连接层的连体网络

时间:2017-12-06 11:30:23

标签: python tensorflow tensorboard tflearn

我正在尝试首次使用tflearn实现一个带有完全连接层的简单暹罗网络。作为数据集,我正在与MNIST合作。我不知道为什么我的代码不起作用。编译器既不显示错误也不显示结果。 对于我所做的任何评论或错误,我将不胜感激。

我的代码:

import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression
import tflearn.datasets.mnist as mnist


train_x1, train_y1, test_x1, test_y1 = mnist.load_data(one_hot=True)
train_x2, train_y2, test_x2, test_y2 = mnist.load_data(one_hot=True)
train_x1 = train_x1.reshape ([-1,28,28,1])
test_x1 = test_x1.reshape ([-1,28,28,1])   
train_x2 = train_x2.reshape ([-1,28,28,1])
test_x2 = test_x2.reshape ([-1,28,28,1])


# define the first network
def tower_network(reuse = False): 
        network = input_data(shape=(None,28,28,1))
        network = conv_2d(network, 32,1, activation='relu',reuse=reuse, scope='conv1')
        network = conv_2d(network, 64,1, activation='relu',reuse=reuse, scope='conv2')
        network = conv_2d(network, 128,1, activation='relu',reuse=reuse, scope='conv3')
        network = max_pool_2d(network, 2, strides=2)
        network = fully_connected(network, 512, activation='relu',reuse=reuse, scope='fc1')
        network = dropout(network, 0.5)
        return network



def similarity_network( net1, net2):
    num_classes = 2
    network = tflearn.merge([net1,net2], mode='concat', axis=1, name='Merge') # merge net1 and net2 networks
    # fully connected layers
    network = tflearn.fully_connected(network, 2048, activation='relu')
    network = tflearn.dropout(network, 0.5)
    network = tflearn.fully_connected(network, 2048, activation='relu')
    network = tflearn.dropout(network, 0.5) 
    # softmax layers
    network = tflearn.fully_connected(network, num_classes, activation='softmax')
    return network




if __name__ == "__main__": 
    net1 = tower_network()
    net2 = tower_network(reuse=True)
    network = similarity_network( net1, net2)
    network = fully_connected(network, 10, activation='softmax')
    network = regression(network, optimizer='adam', learning_rate=0.01, loss='categorical_crossentropy', name='targets')
    model = tflearn.DNN(network, tensorboard_verbose=3)
    model.fit([train_x1,train_x2], [train_y1,train_y2],n_epoch=1)

0 个答案:

没有答案