python - Training in batch does not converge while training samples individually does
问题描述
I'm learning Tensorflow and trying some test models to get the feeling how Tensorflow works. The model is pretty simple (a linear model) and it has the following input and output:
X = tf.placeholder(tf.float32, shape=(None, 1), name="Input")
Y = tf.placeholder(tf.float32, shape=(None, 1), name="Output")
So basically, the input and output have one single dimension. As for training phase:
for epoch in range(training_epochs):
for (x, y) in zip(trX, trY):
sess.run(train_op, feed_dict={X: [x], Y: [y]})
The above code is when I train the model one single sample at a time and it works. As for my next step, I wanted to see how I can train the model with the whole set at each epoch:
for epoch in range(training_epochs):
sess.run(train_op, feed_dict={X: trX, Y: trY})
Training this way does not converge. And trX
& trY
are both of shape (101, 1)
. So is my expectation not in place or am I doing something wrong here?
解决方案
事实证明,问题出在成本函数定义中。最初,成本函数是这样定义的:
cost = tf.pow(Y - y_model, 2)
我意识到它缺少平均分量(前一个是返回一个矩阵而不是单个值)。
cost = tf.reduce_mean(tf.pow(Y - y_model, 2))
推荐阅读
- google-apps-script - 在网页上嵌入 Google 表单并在浏览器内提交时提取响应
- sql - 在一行中检索具有特定条件的一对多关联数据
- amazon-web-services - SES aws 配置的 SMTP 将进入垃圾邮件文件夹
- java - Teamcity 构建后无法获取 Artifacts 文件
- android - 颤振构建apk文件不适用于http请求?
- docker - crond 服务在 docker 容器中不起作用 [图片:amazon linux 2]
- assembly - nasm x86_64 中的 strcmp,寄存器
- python - flask error with .env file then sending mail
- javascript - 通过 AJAX 请求将制表符数据导出为 json 文件
- azure - 以后是否可以从 Azure CLI 查看生成的 json?