Tensorflow中的变量共享

时间:2017-12-02 14:46:32

标签: python tensorflow tensorboard tflearn

我正在尝试首次实施暹罗网络。我对变量共享没有任何经验。我不知道为什么我会成为这个错误“变量conv2 / W不存在,或者不是用tf.get_variable()创建的。你是不是想在VarScope中设置reuse = tf.AUTO_REUSE?”任何帮助表示赞赏

from __future__ import division, print_function, absolute_import

import tensorflow as tf

import tflearn
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.normalization import local_response_normalization
from tflearn.layers.estimator import regression

# Data loading and preprocessing
import tflearn.datasets.mnist as mnist
X, Y, testX, testY = mnist.load_data(one_hot=True)
X = X.reshape([-1, 28, 28, 1])
testX = testX.reshape([-1, 28, 28, 1])


def tower_network(reuse = True):
    network = tflearn.input_data(shape=(None,28,28,1))
    network = tflearn.conv_2d(network, 32,1, activation='relu',reuse=reuse, scope='conv1')
    network = tflearn.conv_2d(network, 64,1, activation='relu',reuse=reuse, scope='conv2') 
    network = tflearn.conv_2d(network, 128,1, activation='relu',reuse=reuse, scope='conv3')

    network = tflearn.max_pool_2d(network, 2, strides=2)

    network = tflearn.fully_connected(network, 512, activation='relu',reuse=reuse, scope='fc1')

    network = tflearn.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




net1 = tower_network()
net2 = tower_network(reuse=True)

#similarity network
network = similarity_network( net1, net2)
#output layer
#network = tflearn.regression(network, optimizer='sgd', loss='hinge_loss', learning_rate=0.02)
network = tflearn.regression(network, optimizer='sgd', loss='categorical_crossentropy', learning_rate=0.02)

# Training
model = tflearn.DNN(network, tensorboard_verbose=0)
model.fit({'input': X}, {'target': Y}, n_epoch=20,
           validation_set=({'input': testX}, {'target': testY}),
snapshot_step=100, show_metric=True, run_id='convnet_mnist')

1 个答案:

答案 0 :(得分:0)

net1 = tower_network()中,参数reuse设置为其默认值True。 这导致tensorflow试图重用具有相同名称的变量,但该变量尚不存在。

net1 = tower_network(reuse=False)替换该行应该可以解决问题。

from __future__ import division, print_function, absolute_import

import tensorflow as tf

import tflearn
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.normalization import local_response_normalization
from tflearn.layers.estimator import regression

# Data loading and preprocessing
import tflearn.datasets.mnist as mnist
X, Y, testX, testY = mnist.load_data(one_hot=True)
X = X.reshape([-1, 28, 28, 1])
testX = testX.reshape([-1, 28, 28, 1])


def tower_network(reuse = True):
    network = tflearn.input_data(shape=(None,28,28,1))
    network = tflearn.conv_2d(network, 32,1, activation='relu',reuse=reuse, scope='conv1')
    network = tflearn.conv_2d(network, 64,1, activation='relu',reuse=reuse, scope='conv2') 
    network = tflearn.conv_2d(network, 128,1, activation='relu',reuse=reuse, scope='conv3')

    network = tflearn.max_pool_2d(network, 2, strides=2)

    network = tflearn.fully_connected(network, 512, activation='relu',reuse=reuse, scope='fc1')

    network = tflearn.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




net1 = tower_network(reuse=False)
net2 = tower_network(reuse=True)

#similarity network
network = similarity_network( net1, net2)
#output layer
#network = tflearn.regression(network, optimizer='sgd', loss='hinge_loss', learning_rate=0.02)
network = tflearn.regression(network, optimizer='sgd', loss='categorical_crossentropy', learning_rate=0.02)

# Training
model = tflearn.DNN(network, tensorboard_verbose=0)
model.fit({'input': X}, {'target': Y}, n_epoch=20,
           validation_set=({'input': testX}, {'target': testY}),
snapshot_step=100, show_metric=True, run_id='convnet_mnist')

这仍然会导致错误的输入错误'输入'你在饲料词典中定义但在其他任何地方都没有,但这是一个不同的问题。