Home > Blockchain >  ValueError: Input 0 of layer conv1d is incompatible with the layer: : expected min_ndim=3, found ndi
ValueError: Input 0 of layer conv1d is incompatible with the layer: : expected min_ndim=3, found ndi

Time:07-14

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

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.

CodePudding user response:

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').

  • Related