简体   繁体   中英

input_shape parameter in Keras/Tensorflow

I do tutorial for machine learning in Tensorflow, with following code:

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. -1.0 and -3.0 for the first epoch or are taken both complete arrays and put into the 1 neuron in the layer?

How it works is that you provide data and output, in your case, xs and 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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