Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
109 views
in Technique[技术] by (71.8m points)

python - How to choose an input_shape in tensorflow?

I'm currently building a model to forcast weather using kaggle Istanbul datasets. I've downloaded and loaded the csv file. I select all the interesting columns, convert them, add code to categorical data and so on.

    df = pd.read_csv('/content/Istanbul Weather Data.csv',sep=",") #store our dataFrame into Df
    df = df[["DateTime","Condition","Rain","MaxTemp","MinTemp","AvgWind","AvgHumidity","AvgPressure"]]
    df["DateTime"] = pd.to_datetime(df["DateTime"].str.replace(".","/"))
    
    #Now let's associate a code to our str data which are categorical :
    #Condition : sunny = 0, Partly cloudy = 1, overcast =2, cloudy=3,  patchy rain possible=4
    #Rain : nothing = 0, rain = 1, snow = 2( for another dataset)
    
    for ind in df.index: 
      if df['Rain'][ind] != 0.0 :
        df['Rain'][ind] = 1.0
      else:
         df['Rain'][ind] = 0.0
      if df['Condition'][ind]== "Sunny":
        df['Condition'][ind] = 0.0
      elif df['Condition'][ind] == "Partly cloudy" :
        df['Condition'][ind] = 1.0
      elif df['Condition'][ind] == "Overcast" :
        df['Condition'][ind] = 2.0
      elif df['Condition'][ind] == "Cloudy" :
        df['Condition'][ind] = 3.0
      else :
        df['Condition'][ind] = 4.0
    
    df['Rain'] = df['Rain'].astype(int)
    df['Condition'] = df['Condition'].astype(int)
    df.head()

Then I extract my features (and convert it into ndarray) and my labels :

    df = df[["Condition","Rain","MaxTemp","MinTemp","AvgWind","AvgHumidity","AvgPressure"]]
    df_features = df.copy()
    df_labels = df_features.pop('Condition')
    
    df_features = np.array(df_features)
    
    df_features

Then I want to write my model :

    model = tf.keras.Sequential([
                 tf.keras.layers.Dense(units=64, input_shape=(6,)),
                 tf.keras.layers.Dense(units=5, activation='softmax')
    ])
    
    model.compile(optimizer=tf.keras.optimizers.Adam(lr=0.01),
                 loss='categorical_crossentropy')
    model.fit(df_features,df_labels,epochs=30)

When I want to fit it I got this error :

    ValueError: Shapes (None, 1) and (None, 5) are incompatible

I don't know how to choose the right input_shape or how to reshape my dataset...

If anyone has a hint I would glad to listen it !

And if you have some recommandations to improve or optimize my model that would be cool if you tell me it !

Thanks in advance !

Regards, Baptiste ZLOCH

question from:https://stackoverflow.com/questions/65887701/how-to-choose-an-input-shape-in-tensorflow

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...