【问题标题】:Keras Input Layer Shape On Input Layer Error输入层错误上的 Keras 输入层形状
【发布时间】:2021-05-20 11:45:33
【问题描述】:

我正在尝试通过构建来学习人工智能算法。我在 Stackoverflow 上发现了一个问题 here。 我复制了这段代码尝试一下,然后修改为这个。

import numpy as np
import tensorflow as tf
from tensorflow import keras as keras
from tensorflow.keras.layers import Dense, Activation
from tensorflow.keras.models import Sequential
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from tensorflow.python.keras import activations
# Importing the dataset
dataset = np.genfromtxt("data.txt", delimiter='')
X = dataset[:, :-1]
y = dataset[:, -1]

# Splitting the dataset into the Training set and Test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.08, random_state = 0)

# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)



# Initialising the ANN
#model = Sequential()

# Adding the input layer and the first hidden layer
#model.add(Dense(32, activation = 'relu', input_dim = 6))

# Adding the second hidden layer
#model.add(Dense(units = 32, activation = 'relu'))

# Adding the third hidden layer
#model.add(Dense(units = 32, activation = 'relu'))

# Adding the output layer

#model.add(Dense(units = 1))
#model = Sequential([
#    keras.Input(shape= (6),name= "digits"),
#    Dense(units = 32, activation = "relu"),
#    Dense(units = 32, activation = "relu"),
#    Dense(units = 1 , name = "predict")##

#])
#
input = keras.Input(shape= (6),name= "digits")
#x0 = Dense(units = 6)(input)
x1 = Dense(units = 32, activation = "relu")(input)
x2 = Dense(units = 32, activation = "relu")(x1)
output = Dense(units = 1 , name = "predict")(x2)

model = keras.Model(inputs = input , outputs= output)
#model.add(Dense(1))
# Compiling the ANN
#model.compile(optimizer = 'adam', loss = 'mean_squared_error')

# Fitting the ANN to the Training set
#model.fit(X_train, y_train, batch_size = 10, epochs = 200)

optimizer = keras.optimizers.Adam(learning_rate=1e-3)
loss = keras.losses.MeanSquaredError()

epochs = 200

for epoch in range(epochs):
    print("\nStart of epoch %d" % (epoch,))

    # Iterate over the batches of the dataset.
    for step in range(len(X_train)):

        # Open a GradientTape to record the operations run
        # during the forward pass, which enables auto-differentiation.
        with tf.GradientTape() as tape:

            # Run the forward pass of the layer.
            # The operations that the layer applies
            # to its inputs are going to be recorded
            # on the GradientTape.
            logits = model( X_train[step] , training=True)  # Logits for this minibatch

            # Compute the loss value for this minibatch.
            loss_value = loss(y_train[step], logits)

        # Use the gradient tape to automatically retrieve
        # the gradients of the trainable variables with respect to the loss.
        grads = tape.gradient(loss_value, model.trainable_weights)

        # Run one step of gradient descent by updating
        # the value of the variables to minimize the loss.
        optimizer.apply_gradients(zip(grads, model.trainable_weights))

        # Log every 200 batches.
        if step % 200 == 0:
            print(
                "Training loss (for one batch) at step %d: %.4f"
                % (step, float(loss_value))
            )
            print("Seen so far: %s samples" % ((step + 1) * 64))


y_pred = model.predict(X_test)

plt.plot(y_test, color = 'red', label = 'Real data')
plt.plot(y_pred, color = 'blue', label = 'Predicted data')
plt.title('Prediction')
plt.legend()
plt.show()

我修改了处理时创建数据的代码。如果我使用model.fit,它会使用我提供的数据,但我想在 epochs 开始从模拟中创建数据然后对其进行处理。(抱歉英语不好。如果我不能很好地解释)

当我在第 81 行开始代码时:

Exception has occurred: ValueError
Input 0 of layer dense is incompatible with the layer: : expected min_ndim=2, found ndim=1. Full shape received: (6,)

它给出了一个例外。我尝试使用 shape=(6,) shape=(6,1) 或类似的,但它不能解决任何问题。

【问题讨论】:

    标签: tensorflow keras


    【解决方案1】:

    调用keras模型时需要添加批量维度:

    logits = model( X_train[step][np.newaxis,:] , training=True)  # Logits for this minibatch
    

    批次维度用于向网络提供多个样本。默认情况下,Keras 假设输入具有批量维度。为了提供一个样本,Keras 需要一批 1 个样本。在这种情况下,它意味着(1,6)的形状。如果要喂一批 2 个样品,那么形状就是 (2,6) 等。

    【讨论】:

    • 是的。那行得通。但是你能再解释一下什么是批处理维度吗?我们用它做什么?
    • 谢谢。当您说样本时,您是在谈论一次插入多少数据或处理了多少数据?我的意思是我有 32 个来自不同溪流的漂浮物来喂食。 (1,32) 好还是 (2,16) 更好?对不起,我试图很好地理解这个愚蠢的问题
    猜你喜欢
    • 1970-01-01
    • 2018-02-27
    • 2017-05-25
    • 1970-01-01
    • 2020-04-29
    • 2019-02-18
    • 1970-01-01
    • 2019-11-13
    • 1970-01-01
    相关资源
    最近更新 更多