小编HSK*_*HSK的帖子

Tensorflow - 如何实现超参数随机搜索?

考虑这个简单的图形+会话定义.假设我想用随机搜索来调整超级参数(学习率和退出保持概率)?实施它的推荐方法是什么?

graph = tf.Graph()
with graph.as_default():

    # Placeholders
    data = tf.placeholder(tf.float32,shape=(None,  img_h, img_w, num_channels),name='data')
    labels = ...
    dropout_keep_prob = tf.placeholder(tf.float32, name='keep_prob')
    learning_rate = tf.placeholder(tf.float32, name='learning_rate')

    # model architecture...

with tf.Session(graph=graph) as session:
    tf.initialize_all_variables().run()
    for step in range(num_steps):
        offset = (step * batch_size) % (train_length.shape[0] - batch_size)
        # Generate a minibatch.
        batch_data = train_images[offset:(offset + batch_size), :]
        #...
        feed_train = {data: batch_data, 
                      #...
                      learning_rate: 0.001,
                      keep_prob : 0.7
                     }
Run Code Online (Sandbox Code Playgroud)

我尝试将所有内容都放在函数中

def run_model(learning_rate,keep_prob):
    graph = tf.Graph()
    with graph.as_default():
    # graph here...

    with …
Run Code Online (Sandbox Code Playgroud)

tensorflow

9
推荐指数
1
解决办法
3210
查看次数

如何使用tf.MonitoredTrainingSession在训练和验证数据集之间切换?

我想feedable在tensorflow Dataset API中使用迭代器设计,所以我可以在一些训练步骤之后切换到验证数据.但如果我切换到验证数据,它将结束整个会话.

以下代码演示了我想要做的事情:

import tensorflow as tf


graph = tf.Graph()
with graph.as_default():
    training_ds = tf.data.Dataset.range(32).batch(4)
    validation_ds = tf.data.Dataset.range(8).batch(4)

    handle = tf.placeholder(tf.string, shape=[])
    iterator = tf.data.Iterator.from_string_handle(
        handle, training_ds.output_types, training_ds.output_shapes)
    next_element = iterator.get_next()

    training_iterator = training_ds.make_initializable_iterator()
    validation_iterator = validation_ds.make_initializable_iterator()


with graph.as_default():

    with tf.train.MonitoredTrainingSession() as sess:
        training_handle = sess.run(training_iterator.string_handle())
        validation_handle = sess.run(validation_iterator.string_handle())
        sess.run(training_iterator.initializer)
        count_training = 0
        while not sess.should_stop():
            x = sess.run(next_element, feed_dict={handle: training_handle})
            count_training += 1
            print('{} [training] {}'.format(count_training, x.shape))
            # print(x)

            # we do periodic validation
            if count_training …
Run Code Online (Sandbox Code Playgroud)

dataset tensorflow tensorflow-datasets tensorflow-estimator

8
推荐指数
1
解决办法
1816
查看次数

TensorFlow 数据集:先洗牌再映射(map_and_batch)?

TensorFlow 提供的股票示例使用mapbefore shuffle,如下所示:

filenames = ["/var/data/file1.tfrecord", "/var/data/file2.tfrecord"]
dataset = tf.data.TFRecordDataset(filenames)
dataset = dataset.map(...)
dataset = dataset.shuffle(buffer_size=10000)
dataset = dataset.batch(32)
Run Code Online (Sandbox Code Playgroud)

但是,性能指南页面GitHub 问题之一表明map_and_batch出于性能原因最好使用。但是由于shuffle卡在中间,我不太确定在那里做什么。看起来shuffle甚至之前申请过map并且batch可以完成工作,如下所示:

filenames = ["/var/data/file1.tfrecord", "/var/data/file2.tfrecord"]
dataset = tf.data.TFRecordDataset(filenames)
dataset = dataset.shuffle(buffer_size=10000)
dataset = dataset.apply(tf.contrib.data.map_and_batch(..., batch_size=32))
Run Code Online (Sandbox Code Playgroud)

我想知道这是否会引入任何我可能没想到的问题,而不是 TensorFlow 提供的股票示例。我希望两个代码做同样的事情,但第二个做的更快;在最坏的情况下以相同的速度。

python tensorflow tensorflow-datasets

4
推荐指数
1
解决办法
2035
查看次数

如果字符串在c中相同则返回0,代码中发生了什么?

我找到了一个与之相同的功能strcmp但我无法看到比较s1 == s2发生的位置.我需要帮助.谢谢.

int MyStrcmp (const char *s1, const char *s2)
{
    int i;
    for (i = 0; s1[i] != 0 && s2[i] != 0; i++)
    {
        if (s1[i] > s2[i])
            return +1;
        if (s1[i] < s2[i])
            return -1;
    }

    if (s1[i] != 0)
        return +1;
    if (s2[i] != 0)
        return -1;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c function strcmp

3
推荐指数
1
解决办法
92
查看次数