tensorflow遇到的错误

1. FailedPreconditionError (see above for traceback): Attempting to use uninitialized value conv2d_1/kernel

	ops.reset_default_graph()
    config = tf.ConfigProto()
    with tf.Session(config=config) as sess:
        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        model = Network(n_H0, n_W0, n_C0, n_y)  #此类中定义了placeholder, cost, forward propagation

        print('Start Training...')
        train(X_train, Y_train, sess, model)  #此函数定义了optimizer = tf.train.AdagradOptimizer(learning_rate=learning_rate).minimize(model.cost)

产生错误:FailedPreconditionError (see above for traceback): Attempting to use uninitialized value conv2d_1/kernel

原因:sess.run(tf.global_variables_initializer()) 全局变量初始化应该位于定义optimizer之后

def train(X_train, Y_train, sess, model, learning_rate=0.009,
          num_epochs=100, minibatch_size=64, print_cost=True):

    costs = []  # To keep track of the cost
    m = X_train.shape[0]

    # Backpropagation: Define the tensorflow optimizer. Use an AdamOptimizer.
    optimizer = tf.train.AdagradOptimizer(learning_rate=learning_rate).minimize(model.cost)

    sess.run(tf.global_variables_initializer())
    sess.run(tf.local_variables_initializer())
    # Do the training loop
    for epoch in range(num_epochs):

        epoch_cost = 0.  # Defines a cost related to an epoch
        num_minibatches = int(m / minibatch_size)  # number of minibatches of size minibatch_size in the train set
        minibatches = random_mini_batches(X_train, Y_train, minibatch_size)

        for minibatch in minibatches:

2. AssertionError: Do not use tf.reset_default_graph() to clear nested graphs. If you need a cleared graph, exit the nesting and create a new graph.

    config = tf.ConfigProto()
    with tf.Session(config=config) as sess:
        ops.reset_default_graph()
        model = Network(n_H0, n_W0, n_C0, n_y)
        print('Start Training...')
        train(X_train, Y_train, sess, model)

产生错误:
AssertionError: Do not use tf.reset_default_graph() to clear nested graphs. If you need a cleared graph, exit the nesting and create a new graph.
原因:
ops.reset_default_graph()不能位于 with tf.Session(config=config) as sess:

	ops.reset_default_graph()
    config = tf.ConfigProto()
    with tf.Session(config=config) as sess:
        model = Network(n_H0, n_W0, n_C0, n_y)
        print('Start Training...')
        train(X_train, Y_train, sess, model)

3.Cannot use the default session to execute operation: the operation’s graph i

graph = tf.Graph()
with graph.as_default():
    train_inputs = tf.placeholder(tf.int32, shape=[batch_size])
    train_labels = tf.placeholder(tf.int32, shape=[batch_size, 1])
    valid_dataset = tf.constant(valid_examples, dtype=tf.int32)

    with tf.device('/cpu:0'):
        embeddings = tf.Variable(tf.random_uniform([vocabulary_size, embedding_size], -1.0, 1.0))
        embed = tf.nn.embedding_lookup(embeddings, train_inputs)

        nce_weights = tf.Variable(tf.truncated_normal([vocabulary_size, embedding_size], stddev=1.0 / math.sqrt(embedding_size)))
        nce_biases = tf.Variable(tf.zeros([vocabulary_size]))

    loss = tf.reduce_mean(tf.nn.nce_loss(weights=nce_weights, biases=nce_biases, labels=train_labels, inputs=embed, num_sampled=num_sampled, num_classes=vocabulary_size))

    optimizer = tf.train.GradientDescentOptimizer(1.0).minimize(loss)

    norm = tf.sqrt(tf.reduce_sum(tf.square(embeddings), 1, keep_dims=True))
    normalized_embeddings = embeddings/norm
    valid_embeddings = tf.nn.embedding_lookup(normalized_embeddings, valid_dataset)
    similarity = tf.matmul(valid_embeddings, normalized_embeddings, transpose_b=True)

    init = tf.global_variables_initializer()

num_steps = 100001

with tf.Session() as session:
    init.run()

修改:

with tf.Session() as session:

4. 解决Python模块报错:ModuleNotFoundError: No module named ‘cStringIO’

从Python 3.0开始,StringIO和cStringIO模块已经取消。通过import io模块代替,分别使用io.String或io.BytesIO处理文本和数据。从Python 3邮件流文档能看到相关实现StringIO的代码为:

5. tf.flags

读别人家的代码的时候经常看到这个,结果两三天不看居然忘记了,这脑子绝对上锈了,决定记下来免得老是查来查去的。。。
内容包含如下几个我们经常看到的几个函数:
①tf.flags.DEFINE_xxx() #全局参数设置,显示在命令行 #第一个是参数名称,第二个参数是默认值,第三个是参数描述
②FLAGS = tf.flags.FLAGS #FLAGS保存命令行参数的数据
③FLAGS._parse_flags() #将其解析成字典存储到FLAGS.__flags中
简单的说:
用于帮助我们添加命令行的可选参数。
也就是说利用该函数我们可以实现在命令行中选择需要设定的参数来运行程序,
可以不用反复修改源代码中的参数,直接在命令行中进行参数的设定。

##6. ‘TypeError: main() takes no arguments (1 given)’
修改

def main(_):  
    print(FLAGS.str_name)
    print(FLAGS.int_name)
    print(FLAGS.bool_name)

if __name__ == '__main__':
    tf.app.run()  #执行main函数

你可能感兴趣的:(tensorflow)