0
votes

I am trying to expand dimension:

import tensorflow as tf
inp = tf.keras.layers.Input(shape=(1,))
inp = inp[..., tf.newaxis]
decoder_input = inp
output = tf.concat([inp, decoder_input], 1)
model = tf.keras.models.Model(inp, output )

But I get an error in the last line:

Exception has occurred: ValueError Graph disconnected: cannot obtain value for tensor Tensor("input_1:0", shape=(None, 1), dtype=float32) at layer "tf_op_layer_strided_slice". The following previous layers were accessed without issue: []

2
I can't understand what you are expecting the model to do. Does the 'expand dimension' mean reshaping from shape (a,b)->(a,b,1) or something? - krenerd
@krenerd yes, reshaping (a, b) -> (a, b, 1) - Andrey
Use tf.expand_dims instead. - today
@today I tryed - not working as well - Andrey
How did you use tf.expand_dims? Edit your question and add the code snippet. Probably you are still modifying the inp (which you shouldn't at all because that's the input of the model and should not be modified). - today

2 Answers

1
votes

Is this what you are trying to do? Seems you have a variable conflict. You are setting the decoder_input as a reshape layer instead of an input layer. Changing the name of the reshape layer fixes it.

import tensorflow as tf
inp = tf.keras.layers.Input(shape=(1,))

x = tf.keras.layers.Reshape((-1,1))(inp) #Use any of the 3
#x = tf.expand_dims(inp, axis=-1)
#x = inp[...,tf.newaxis]

decoder_input = inp
output = tf.concat([inp, decoder_input], 1)
model = tf.keras.models.Model(inp, output)

model.summary()
Model: "functional_8"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_7 (InputLayer)            [(None, 1)]          0                                            
__________________________________________________________________________________________________
tf_op_layer_concat_4 (TensorFlo [(None, 2)]          0           input_7[0][0]                    
                                                                 input_7[0][0]                    
==================================================================================================
Total params: 0
Trainable params: 0
Non-trainable params: 0
0
votes

If you are wanting the model to reshape tensors from (a, b) -> (a, b, 1), you can use the tf.keras.layers.Reshape layer.

inp = tf.keras.layers.Input(shape=(1,))
...
decoder_input = inp
output=tf.keras.layers.Reshape((a,b,1))(decoder_input ) #Replace (a,b,1) with your desired shape.
...