目前,我尝试使用Tensorflow的新Estimator API在自定义图像数据集上训练自动编码器.
到目前为止一切正常.我唯一的问题是当模型处于评估模式时将输入和输出图像保存为摘要.我在列车模式下创建的所有图像摘要都存储在Tensorboard中并正确显示.
这是我的代码:
def model_fn_autoencoder(features, labels, mode, params):
is_training = mode == ModeKeys.TRAIN
# Define model's architecture
logits = architecture_autoencoder(features, is_training=is_training)
# Loss, training and eval operations are not needed during inference.
loss = None
train_op = None
#eval_metric_ops = {}
if mode != ModeKeys.INFER:
loss = tf.reduce_mean(tf.square(logits - features))
train_op = get_train_op_fn(loss, params)
#eval_metric_ops = get_eval_metric_ops(labels, predictions)
if mode == ModeKeys.TRAIN:
for i in range(10):
tf.summary.image("Input/Train/" + str(i), tf.reshape(features[i],[1, 150, 150, 3]))
tf.summary.image("Output/Train/" + str(i), tf.reshape(logits[i],[1, 150, 150, …Run Code Online (Sandbox Code Playgroud) TF1.4使Keras成为不可或缺的一部分.当尝试使用具有propratery输入功能的Keras模型创建Estimators时(即,不使用tf.estimator.inputs.numpy_input_fn)事情不起作用,因为Tensorflow无法将模型与Input函数融合.
我使用的是tf.keras.estimator.model_to_estimator
keras_estimator = tf.keras.estimator.model_to_estimator(
keras_model = keras_model,
config = run_config)
train_spec = tf.estimator.TrainSpec(input_fn=train_input_fn,
max_steps=self.train_steps)
eval_spec = tf.estimator.EvalSpec(input_fn=eval_input_fn,
steps=None)
tf.estimator.train_and_evaluate(keras_estimator, train_spec, eval_spec)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误消息:
Cannot find %s with name "%s" in Keras Model. It needs to match '
'one of the following:
Run Code Online (Sandbox Code Playgroud)
我在这里找到了一些关于这个主题的参考资料(奇怪的是它隐藏在主分支的TF文档中 - 与此相比)
如果您有同样的问题 - 请参阅下面的答案.可能会为您节省几个小时.
我正在寻找一种方法来实现学习率的搜索,如下所述:https://arxiv.org/pdf/1506.01186.pdf.
我的网络使用estimator api实现,我想坚持这一点,但遗憾的是我无法强制估算器跳过保存检查点.您是否知道如何在不保存检查点的情况下简单地进行一次训练?
我猜想DNN在某种意义上是TensorFlow指“ 深度神经网络 ”。但是我发现这深深地令人困惑,因为“深度”神经网络的概念似乎在其他地方被广泛使用,这意味着通常具有多个卷积和/或关联层(ReLU,池化,丢失等)的网络。
相比之下,很多人会首先遇到这个术语(在tfEstimator Quickstart示例代码中),我们发现:
# Build 3 layer DNN with 10, 20, 10 units respectively.
classifier = tf.estimator.DNNClassifier(feature_columns=feature_columns,
hidden_units=[10, 20, 10],
n_classes=3,
model_dir="/tmp/iris_model")
Run Code Online (Sandbox Code Playgroud)
这听起来像是可疑的浅,甚至更像是老式的多层感知器(MLP)网络。但是,没有提到DNN该近似定义源上的替代术语。那么DNN在TensorFlow tf.estimator上下文中实际上是一个MLP吗?hidden_units参数的文档表明是这种情况:
MLP已经写满了。这种理解正确吗?DNN因此,是否使用了不当用语?如果DNNClassifier可以,则最好弃用MLPClassifier?还是DNN代表深层神经网络以外的东西?
我正在尝试DNNClassifier在张量流中训练
这是我的代码
train_input_fn = tf.estimator.inputs.pandas_input_fn(
x=X_train,
y=y_train,
batch_size=1000,
shuffle = True
)
nn_classifier = tf.estimator.DNNClassifier(hidden_units=[1300,1300,1300], feature_columns=X_train, n_classes=200)
nn_classifier.train(input_fn = train_input_fn, steps=2000)
Run Code Online (Sandbox Code Playgroud)
这是y_train看起来
[450 450 450 ... 327 327 327]
Run Code Online (Sandbox Code Playgroud)
类型:numpy.ndarray
这是X_train看起来
[[ 9.79285 11.659035 1.279528 ... 1.258979 1.063923 -2.45522 ]
[ 8.711333 13.92955 1.117603 ... 3.588921 1.231256 -3.180302]
[ 5.159803 14.059619 1.740708 ... 0.28172 -0.506701 -1.326669]
...
[ 2.418473 0.542642 -3.658447 ... 4.631474 4.544892 -4.595605]
[ 6.51176 4.321688 -1.483697 ... 3.13299 5.476103 -2.833903] …Run Code Online (Sandbox Code Playgroud) 我真的对tf.estimator.inputs.numpy_input_fn 这里的tensorflow估计器的文档感到困惑,尤其是关于以下内容的行num_epochs:
num_epochs: Integer, number of epochs to iterate over data. If None will run forever.
如果我设置num_epochs到None,培训会永远运行下去?
它永远运行意味着什么?
这对我来说没有意义,因为我无法想象人们会以可能永远运行的方式设计程序。
有人可以解释吗?
回答我自己的问题:我想我已经在这里找到答案了:https : //www.tensorflow.org/versions/r1.3/get_started/input_fn#evaluating_the_model
具体来说,在该部分中Building the input_fn:
Two additional arguments are provided: num_epochs: controls the number of epochs to iterate over data. For training, set this to None, so the
input_fn keeps returning data until the required number of train steps is reached. For evaluate and predict, set this to …
所有官方的google教程都对所有estimator api实现使用一次射击迭代器,我找不到任何有关如何使用tf.data的可初始化迭代器和可重新初始化迭代器的文档,而不是一次射击迭代器。
有人可以告诉我如何使用tf.data的可初始化迭代器和可重新初始化插入器在train_data和test_data之间切换。我们需要运行一个会话来使用feed dict,并在可初始化的迭代器,其低级api及其令人困惑的如何使用它的方法中切换数据集,该方法是估算器api体系结构的一部分
PS:我确实发现Google提到“注意:目前,单次迭代器是唯一可与Estimator一起使用的类型。”
但是社区内部有什么工作吗?还是我们应该出于某种原因坚持使用一次射击迭代器
时间相关数据我最初的格式为整数:
1234 # corresponds to 12:34
2359 # corresponds to 23:59
Run Code Online (Sandbox Code Playgroud)
1)第一个选项是将时间描述为numeric_column:
tf.feature_column.numeric_column(key="start_time", dtype=tf.int32)
Run Code Online (Sandbox Code Playgroud)
2)另一种选择是将时间分成几小时和几分钟分成两个独立的特征列:
tf.feature_column.numeric_column(key="start_time_hours", dtype=tf.int32)
tf.feature_column.numeric_column(key="start_time_minutes", dtype=tf.int32)
Run Code Online (Sandbox Code Playgroud)
3)第三个选项是维护一个特征列,但让tensorflow知道它可以在分成小时和分钟时进行描述:
tf.feature_column.numeric_column(key="start_time", shape=2, dtype=tf.int32)
Run Code Online (Sandbox Code Playgroud)
这种分裂是否有意义,选项2)和3)之间有什么区别?
另外一个问题,我遇到了如何从csv解码矢量数据的问题:
1|1|FGTR|1|1|14,2|15,1|329|3|10|2013
1|1|LKJG|1|1|7,2|19,2|479|7|10|2013
1|1|LKJH|1|1|14,2|22,2|500|3|10|2013
Run Code Online (Sandbox Code Playgroud)
如何让tensorflow知道"14,2","15,1"应该被视为张量形状= 2?
编辑1:
我找到了解码来解码来自csv的"数组"数据的解决方案.在训练和评估函数中,我添加.map了一些步骤来解码某些列的数据:
dataset = tf.data.Dataset.from_tensor_slices((dict(features), labels)).map(parse_csv)
Run Code Online (Sandbox Code Playgroud)
parse_csv实现为:
def parse_csv(features, label):
features['start_time'] = tf.string_to_number(tf.string_split([features['start_time']], delimiter=',').values, tf.int32)
return features, label
Run Code Online (Sandbox Code Playgroud)
我认为两个分离的列和一个列之间的区别在于shape=2"权重"的分布方式.
我试图将Tensorflow的官方基本word2vec实现转换为使用tf.Estimator.问题是当使用Tensorflow Estimators时,损失函数(sampled_softmax_loss或nce_loss)会出错.它在原始实现中完美地运行.
这是Tensorflow的官方基本word2vec实现:
以下是我实施此代码的Google Colab笔记本,该代码正常运行.
https://colab.research.google.com/drive/1nTX77dRBHmXx6PEF5pmYpkIVxj_TqT5I
这是Google Colab笔记本,我在其中更改了代码,因此它使用Tensorflow Estimator,它不起作用.
https://colab.research.google.com/drive/1IVDqGwMx6BK5-Bgrw190jqHU6tt3ZR3e
为方便起见,这里是我定义的Estimator版本的精确代码 model_fn
batch_size = 128
embedding_size = 128 # Dimension of the embedding vector.
skip_window = 1 # How many words to consider left and right.
num_skips = 2 # How many times to reuse an input to generate a label.
num_sampled = 64 # Number of negative examples to sample.
def my_model( features, labels, mode, params):
with tf.name_scope('inputs'):
train_inputs = features
train_labels = labels …Run Code Online (Sandbox Code Playgroud) 我在以下TF Hub上按照BERT预测电影评论的步骤进行了操作:https : //colab.research.google.com/github/google-research/bert/blob/master/predicting_movie_reviews_with_bert_on_tf_hub.ipynb#scrollTo=PPVEXhNjYXC-。
最后,如何导出模型以供以后用作分类器?
我找到了一个链接(https://guillaumegenthial.github.io/serving-tensorflow-estimator.html),该链接表明我可以将估算器导出为tf.saved_model。但是,我在创建“ serving_input_receiver_fn()”时遇到了麻烦。