9
$\begingroup$

Is this telling the model that there are two dimensions (i.e. it’s a matrix) but we don’t yet know the size of that particular dimension? If so, how can the model be compiled? Doesn’t the size of each dimension affect the number of nodes in middle layers?

$\endgroup$

2 Answers 2

12
$\begingroup$

In tf.keras, a None dimension means that it can be any scalar number, so that you use this model to infer on an arbitrarily long input. This dimension does not affect the size of the network, it just denotes that you are free to select the length (number of samples) of your input during testing.

$\endgroup$
3
$\begingroup$

You are almost right. However, in your specific examples (None,) and (None,12), we actually do know the size of the model's input and that's why the model can be compiled. (None,) refers to scalar inputs and (None,12) refers to 12-dimensional input vectors. Therefore, one can think of None as an adjustable variable/placeholder for the batch size of your model's optimization algorithm.

A more specific example: if the batch-size during model optimization/training is 32, then None will take one the value 32 and your model's input will be of size (32,12).

$\endgroup$
2
  • $\begingroup$ Why do we write (None,) instead of just (None)? $\endgroup$
    – skan
    Commented Aug 10, 2023 at 17:31
  • $\begingroup$ When you see a trailing comma after a single element inside parentheses, it indicates that you are creating a tuple with a single element. This is because parentheses are also used for grouping expressions, and without the comma, Python would interpret it as an expression enclosed in parentheses, rather than a tuple with a single element. For example: 1. (None) is just an expression with the value None. 2. (None,) is a tuple containing a single element, which is None. $\endgroup$ Commented Aug 24, 2023 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.