Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ValueError: Input 0 of layer conv1d is incompatible with the layer: : expected min_ndim=3, found ndim=2

So I was tinkering with some code for time series forecasting. I have dealt with this error before (the formatting of my data was wrong). But in this case I can’t figure out what I’ve done wrong.
Here is the source of the problem

    monk= tf.keras.models.Sequential()
    monk.add(tf.keras.layers.Flatten())
    monk.add(tf.keras.layers.Conv1D(64,2,input_shape=(X_train.shape[1],X_train.shape[2])))
    monk.add(tf.keras.layers.MaxPool1D())
    monk.add(tf.keras.layers.Activation('relu'))
    monk.add(tf.keras.layers.Dense(32))
    monk.add(tf.keras.layers.Dense(1,'sigmoid'))
    monk.compile('adam','binary_crossentropy',['accuracy'])
    monk.fit(X_train,y_train,epochs=10)

where the shape of X_train is (100,5,1) and the shape of y_train is (100,)

The fully reproducible code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

from random import shuffle
from torch import are_deterministic_algorithms_enabled
import yfinance as yf
import tensorflow as tf
import datetime 
import time
import numpy as np
def retrain(symbol):

    todayy  = [int(item) for item in str(datetime.datetime.today()).split(' ')[0].split('-')]
    start = datetime.datetime(todayy[0]-2,todayy[1],todayy[2])
    end = datetime.datetime(todayy[0],todayy[1],todayy[2])
    stock = yf.download(symbol,start=start,end=end)
    print(stock)
    buy = []
    for x in range(stock.shape[0]):
        open = stock.iloc[x]['Open']
        close=stock.iloc[x]['Close']
        if close-open>0:
            buy.append(1)
        else:
            buy.append(0)
    print(buy)
    X = []
    y= []
    temp=[]
    for x in range(len(buy)):
        item = buy[x]
        temp.append(np.array([item]))
        if len(temp)>=5:
            X.append(np.array(temp))
            temp=[]
            try:
                y.append(buy[x+1])
            except: 
                break
    buyz=[]
    sellz=[]
    for item in list(zip(X,y)):
        print(item)
        if item[1]==1:
            buyz.append(item)
        else:
            sellz.append(item)
    
    buyz = buyz[:min(len(buyz),len(sellz))]
    selzz = sellz[:min(len(buyz),len(sellz))]
    
    all = []
    for item in buyz:
        all.append(item)
    for item in sellz:
        all.append(item)
    shuffle(all)
    X_train = []
    y_train =[]
    for item in all:
        print(item)
        X_train.append(item[0])
        y_train.append(item[1])
    #input()
    X_train=np.array(X_train)
    y_train=np.array(y_train)
    print(X_train)
    print(y_train)
    print(X_train.shape)
    print(y_train.shape)
    monk= tf.keras.models.Sequential()
    monk.add(tf.keras.layers.Flatten())
    monk.add(tf.keras.layers.Conv1D(64,2,input_shape=(X_train.shape[1],X_train.shape[2])))
    monk.add(tf.keras.layers.MaxPool1D())
    monk.add(tf.keras.layers.Activation('relu'))
    monk.add(tf.keras.layers.Dense(32))
    monk.add(tf.keras.layers.Dense(1,'sigmoid'))
    monk.compile('adam','binary_crossentropy',['accuracy'])
    monk.fit(X_train,y_train,epochs=10)
    #print(monk(X))
            
        
retrain('LEVI')
 

Any help would be much appreciated.

>Solution :

Remove tf.keras.layers.Flatten(), since it is flattening your 3D tensor (batch size, timesteps, features) to (batch size, features).

You can should add the Flatten layer again after tf.keras.layers.Activation('relu').

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading