简体   繁体   English

Keras/Tensorflow 中的 input_shape 参数

[英]input_shape parameter in Keras/Tensorflow

I do tutorial for machine learning in Tensorflow, with following code:我在 Tensorflow 做机器学习教程,代码如下:

import tensorflow as tf
import numpy as np
from tensorflow import keras
 
model = tf.keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])])
model.summary()
 
model.compile(optimizer='sgd', loss='mean_squared_error')
 
xs = np.array([-1.0,  0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)
ys = np.array([-3.0, -1.0, 1.0, 3.0, 5.0, 7.0], dtype=float)
 
model.fit(xs, ys, epochs=50)
 
print(model.predict([10.0]))

It works fine but I struggle with understanding what data are taken as an input for each epoch run.它工作得很好,但我很难理解每次运行时将哪些数据作为输入。 Input data are two arrays of numbers, model.summary() call shows that model expect two inputs but I do not understand what exactly is that input - is it eg.输入数据是两个 arrays 数字, model.summary() 调用显示 model 期望两个输入,但我不明白那个输入到底是什么 - 是吗? -1.0 and -3.0 for the first epoch or are taken both complete arrays and put into the 1 neuron in the layer? -1.0 和 -3.0 用于第一个 epoch 还是同时完成 arrays 并放入层中的 1 个神经元?

How it works is that you provide data and output, in your case, xs and ys.它的工作原理是您提供数据和 output,在您的情况下是 xs 和 ys。 The network will take it batchwise.网络将分批接收。 If your batch size is 1, it will first take xs[0] and ys[0], then backpropagate, then, the next.如果您的批量大小为 1,它将首先采用 xs[0] 和 ys[0],然后反向传播,然后是下一个。 If batch size is more than 1, the array according to batch will go, let's say, your batch size is 4, then, first xs[:4] and ys[:4] will go through the network, then, the backpropagation will happen.如果batch size大于1,则根据batch的数组会是go,比方说,你的batch size是4,那么,首先xs[:4]和ys[:4]会通过网络go,然后,反向传播将发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM