0

I have a numpy array having the 512 data points, I want to consider this as the input to my 1d convolutional model. After training the model in unsupervised way I want to predict the same input data from the model and want to save to a file

inp_data can be calculated as

import numpy as np
inp_data = np.random.normal(0, .1, 512)

So I did something like

inp_data = np.expand_dims(inp_data, axis=1) #512,1


img_width = inp_data.shape[1]
img_height = inp_data.shape[0]
input_img = Input((img_height,img_width))

print(input_img.shape) #(None, 512, 1)

x = Conv1D(256, 16, activation='relu', padding='same', strides=2)(input_img)
x = Conv1D(128, 16, activation='relu', padding='same',strides=2)(x)
x = BatchNormalization(name='bn1')(x)
x = Conv1D(64, 16, activation='relu', padding='same',strides=2 )(x)
encoded =Conv1D(32, 16, activation='relu', padding='same',strides=2 )(x)



x = Conv1DTranspose(32, 16,activation='relu', padding='same',strides=2 )(encoded)
x = Conv1DTranspose(64, 16, activation='relu', padding='same',strides=2 )(x)
x = BatchNormalization(name='bn3')(x)
x = Conv1DTranspose(128, 16, activation='relu', padding='same',strides=2)(x)
x = Conv1DTranspose(256, kernel_size=16, activation='relu', padding='same')(x) 

decoded = Conv1DTranspose(1, 16, activation='sigmoid', padding='same',strides=2)(x)

autoencoder = Model(input_img, decoded)
sgd = tf.keras.optimizers.Adam(lr=0.001)
autoencoder.compile(optimizer=sgd, loss='mse')

autoencoder.summary()
batch = 1
history = autoencoder.fit(inp_data,inp_data, epochs=50, batch_size=batch, shuffle=True)



out = autoencoder.predict(inp_data)
print(out.shape)
np.savetxt('outfile',out)

However I am getting Errors:

WARNING:tensorflow:Model was constructed with shape (None, 512, 1) for input KerasTensor(type_spec=TensorSpec(shape=(None, 512, 1), dtype=tf.float32, name='input_1'), name='input_1', description="created by layer 'input_1'"), but it was called on an input with incompatible shape (32, 1, 1).
(512, 16, 1)

and while trying to save the predicted output to a file I am getting errors like

np.savetxt('outfile',out)
  File "<__array_function__ internals>", line 6, in savetxt
  File "/home/yuanj/SFTR/anaconda3/envs/tfenv/lib/python3.7/site-packages/numpy/lib/npyio.py", line 1383, in savetxt
    "Expected 1D or 2D array, got %dD array instead" % X.ndim)
ValueError: Expected 1D or 2D array, got 3D array instead

I hope experts may help me overcoming this problem.Thanks.

0 Answers0