When I use the cnn-lstm model, after training, I want to extract features and then reduce dimensionality for visualization, I can’t extract them

# 提取特定参数的数据
x_downsampled = x[downsampled_indices][:, :, selected_params]
y_downsampled = y[downsampled_indices]

# 数据标准化
scaler = MinMaxScaler()
n_samples, time_steps, n_features = x_downsampled.shape
x_downsampled_flat = x_downsampled.reshape(n_samples, -1)
x_downsampled_flat = scaler.fit_transform(x_downsampled_flat)
x_downsampled = x_downsampled_flat.reshape(n_samples, time_steps, n_features)

# 划分训练集和测试集
x_train, x_test, y_train, y_test = train_test_split(x_downsampled, y_downsampled, test_size=0.2, random_state=42)

# 定义CNN-LSTM模型
model = Sequential()
model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(time_steps, n_features)))
model.add(MaxPooling1D(pool_size=2))
model.add(LSTM(64, return_sequences=True))
model.add(LSTM(64, return_sequences=False))
model.add(Dense(4, activation='softmax'))  # 假设有4个类别的输出

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# 打印模型摘要以检查层次结构
model.summary()

# 训练模型
model.fit(x_train, y_train, epochs=10, batch_size=64, validation_split=0.2)

feature_extractor = Model(inputs=model.input, outputs=model.layers[3].output)
# 提取特征
x_train_features = feature_extractor.predict(x_train)
x_test_features = feature_extractor.predict(x_test)

# 使用t-SNE对特征进行降维
tsne = TSNE(n_components=2, random_state=42)
features_2d = tsne.fit_transform(np.vstack((x_train_features, x_test_features)))

# 可视化
plt.figure(figsize=(10, 6))
plt.scatter(features_2d[:x_train_features.shape[0], 0], features_2d[:x_train_features.shape[0], 1], c=y_train, cmap='viridis', label='Training Data')
plt.scatter(features_2d[x_train_features.shape[0]:, 0], features_2d[x_train_features.shape[0]:, 1], c=y_test, cmap='viridis', label='Test Data')
plt.colorbar(label='Classes')
plt.title('t-SNE Visualization of CNN-LSTM Features')
plt.xlabel('Dimension 1')
plt.ylabel('Dimension 2')
plt.legend()
plt.show()

Then it keeps throwing errors like this one:ValueError: The layer sequential_4 has never been called and thus has no defined input.
I really don’t know how to solve it.

# 创建一个新模型,以获取中间层的输出
intermediate_layer_model = tf.keras.Model(inputs=model.input,
                                          outputs=[model.get_layer('dense_1').output])

# 获取中间层输出
intermediate_output = intermediate_layer_model.predict(x_downsampled)


# 对原始输入和中间层输出进行 t-SNE 降维
x_downsampled_flat = x_downsampled.reshape(n_samples, -1)
tsne = TSNE(n_components=2, random_state=42)

x_tsne = tsne.fit_transform(x_downsampled_flat)
intermediate_output_tsne = tsne.fit_transform(intermediate_output)


def plot_tsne(data, labels, title):
    plt.figure(figsize=(8, 8))
    for label in np.unique(labels):
        indices = np.where(labels == label)
        plt.scatter(data[indices, 0], data[indices, 1], label=f'Class {label}', alpha=0.6)
    plt.legend()
    plt.title(title)
    plt.show()

# 可视化原始输入数据的 t-SNE 降维结果
plot_tsne(x_tsne, y_downsampled, 't-SNE of Original Input Data')

# 可视化中间层输出数据的 t-SNE 降维结果
plot_tsne(intermediate_output_tsne, y_downsampled, 't-SNE of Dense Layer Output Data')

I just want to extract the data from the output layer, but it is unsuccessful.I want to know why the error is happening.maybe i know little about what i’m doing.

New contributor

rain holmes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật