tensorflow中np.vstack()用法

本文详细介绍了在TensorFlow环境中使用numpy库的vstack()函数进行数组垂直堆叠的方法。通过实例演示了如何创建两个一维数组,并利用vstack()函数将其在垂直方向上进行合并,最终得到二维数组。
部署运行你感兴趣的模型镜像

tensorflow中np.vstack()用法

import numpy as np

np.vstack(array1, array2) #将数组1:array1和数组2:array2垂直方向相加

 

 np.vstack()用法

import numpy as np #导入numpy模块
a = np.array([1, 2, 3]) #新建数组a

b = np.array([4, 5, 6]) #新建数组b

c = np.vstack((a, b)) #使用numpy中vstack()方法,将数组a,和数组b垂直方向上叠加

print(c)

 结果为:

[[1 2 3]
 [4 5 6]]

您可能感兴趣的与本文相关的镜像

TensorFlow-v2.15

TensorFlow-v2.15

TensorFlow

TensorFlow 是由Google Brain 团队开发的开源机器学习框架,广泛应用于深度学习研究和生产环境。 它提供了一个灵活的平台,用于构建和训练各种机器学习模型

``` import tensorflow as tf from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Dropout from tensorflow.keras.optimizers import Adam import numpy as np import glob import pandas as pd # 加载数据函数 def load_dataset(base_folder): datasets = [] labels = [] for category in ['内圈故障', '球故障']: files = glob.glob(f'{base_folder}/{category}/*.csv') for file in files: df = pd.read_csv(file).to_numpy() datasets.append(df.astype(np.float32)) labels.append(category == '内圈故障') # 内圈故障标记为True(1),其他为False(0) return np.vstack(datasets), np.hstack(labels) # 构建分类模型 def build_classifier_model(input_dim): model = Sequential([ Dense(128, activation='relu', input_shape=(input_dim,)), Dropout(0.5), Dense(64, activation='relu'), Dropout(0.5), Dense(1, activation='sigmoid') # 分类任务 ]) model.compile(optimizer=Adam(), loss='binary_crossentropy', metrics=['accuracy']) return model # 构建领域判别器 def build_discriminator_model(input_dim): model = Sequential([ Dense(64, activation='relu', input_shape=(input_dim,)), Dropout(0.5), Dense(1, activation='sigmoid') # 域分类任务 ]) model.compile(optimizer=Adam(), loss='binary_crossentropy', metrics=['accuracy']) return model if __name__ == '__main__': # 载入训练集和验证集的数据 x_train, y_train = load_dataset('./划分后的数据/训练集') x_val, _ = load_dataset('./划分后的数据/验证集') feature_extractor = Sequential([Dense(128, activation='relu'), Dense(64, activation='relu')]) classifier = build_classifier_model(x_train.shape[1]) discriminator = build_discriminator_model(64) combined_input = feature_extractor(x_train[:]) # 提取特征 domain_labels = np.concatenate([np.ones(len(combined_input) // 2), np.zeros(len(combined_input) // 2)]) # 标记来源域 # 训练过程省略...需要交替优化分类损失和域混淆损失 print("Domain adaptation with DDC completed.")```再次修改后的结果:Traceback (most recent call last): File "C:/Users/Lenovo/AppData/Roaming/JetBrains/PyCharmCE2020.2/scratches/scratch_19.py", line 89, in <module> cm = confusion_matrix(y_test, pred_labels) File "C:\Users\Lenovo\PycharmProjects\pythonProject1\venv\lib\site-packages\sklearn\metrics\_classification.py", line 307, in confusion_matrix y_type, y_true, y_pred = _check_targets(y_true, y_pred) File "C:\Users\Lenovo\PycharmProjects\pythonProject1\venv\lib\site-packages\sklearn\metrics\_classification.py", line 84, in _check_targets check_consistent_length(y_true, y_pred) File "C:\Users\Lenovo\PycharmProjects\pythonProject1\venv\lib\site-packages\sklearn\utils\validation.py", line 334, in check_consistent_length % [int(l) for l in lengths] ValueError: Found input variables with inconsistent numbers of samples: [98, 97902]
03-30
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值