# 读取输出数据 # 读取train.hdf5文件中的二维数组 with h5py.File('train001.hdf5', 'r') as f: data01 = f['increment_4/phase/alpha-Ti/mechanical/O'][:] data02 = f['/increment_4/phase/alpha-Ti/mechanical/epsilon_V^0.0(F)_vM'][:] data02 = data02[:, np.newaxis] data03 = f['/increment_4/phase/alpha-Ti/mechanical/sigma_vM'][:] data03 = data03[:, np.newaxis] data03 = data03 / 1e6 # 归一化到-1和1之间 data03 = (data03 - np.max(data03) / 2) / (np.max(data03) / 2) # 按行连接数组 arr = np.hstack((data01, data02, data03)) # 每1024行提取出来作为一个二维数组 output_arr_reshaped = arr.reshape((-1, 1024, 6)) # 将每个二维数组转换成三维数组 output_arr_3d = [] for i in range(output_arr_reshaped.shape[0]): output_arr_3d.append(np.reshape(output_arr_reshaped[i], (32, 32, 6))) # 将每个三维数组转换成6 * 32 * 32的三维数组 output_arr_6_32_32 = [] for i in range(len(output_arr_3d)): output_arr_6_32_32.append(np.transpose(output_arr_3d[i], (2, 0, 1))) # 对每个5 * 32 * 32的数组进行修改 modified_output_arr = [] for i in range(len(output_arr_6_32_32)): output_arr_i = output_arr_6_32_32[i] output_arr_i = np.array(output_arr_i) output_arr_i = torch.from_numpy(output_arr_i).float() output_arr_i = output_arr_i.unsqueeze(0) modified_output_arr.append(output_arr_i) # 输出第一个三维数组中的第一个纵向二维数组 output_arr01 = modified_output_arr[0] 帮我优化这段代码,使得这段代码功能完全不变的情况下更加精简,并帮我标好每一段的功能
时间: 2023-06-13 22:07:27 浏览: 104
h5py_test.rar_.h5文件 h5py_h5文件读取_hdf_ruleq77_读取H5数据
# 读取train.hdf5文件中的二维数组
import h5py
with h5py.File('train001.hdf5', 'r') as f:
# 读取三个二维数组
data01 = f['increment_4/phase/alpha-Ti/mechanical/O'][:]
data02 = f['/increment_4/phase/alpha-Ti/mechanical/epsilon_V^0.0(F)_vM'][:, np.newaxis]
data03 = f['/increment_4/phase/alpha-Ti/mechanical/sigma_vM'][:, np.newaxis] / 1e6
# 将三个二维数组按行连接成一个二维数组
arr = np.hstack((data01, data02, data03))
# 将每1024行提取出来作为一个三维数组
output_arr_3d = [np.reshape(arr[i*1024:(i+1)*1024], (32, 32, 6)) for i in range(arr.shape[0]//1024)]
# 将每个三维数组转换成6 * 32 * 32的三维数组
output_arr_6_32_32 = [np.transpose(output_arr_3d[i], (2, 0, 1)) for i in range(len(output_arr_3d))]
# 将每个三维数组转换成PyTorch tensor,并添加一个维度
modified_output_arr = [torch.from_numpy(output_arr_6_32_32[i]).float().unsqueeze(0) for i in range(len(output_arr_6_32_32))]
# 输出第一个三维数组中的第一个纵向二维数组
output_arr01 = modified_output_arr[0]
阅读全文