【问题标题】:Only integers, slices (`:`), ellipsis (`...`), tf.newaxis (`None`) and scalar tf.int32/tf.int64 tensors are valid indices, got array([ 219, 928,只有整数、切片 (`:`)、省略号 (`...`)、tf.newaxis (`None`) 和标量 tf.int32/tf.int64 张量是有效索引,得到数组([ 219, 928,
【发布时间】:2021-12-05 08:42:27
【问题描述】:

我有三个不同的传感器。对于每个我根据 tf 具有以下形状:

sensor1.shape = (1134, 100, 400)
sensor2.shape = (1134, 100, 400)
sensor3.shape = (1134, 100, 400)

每个张量都有相同的大小。

在下一步中,我将这 3 个传感器堆叠起来,以便获得一个只有 3 个维度的传感器:

final = tf.stack([sensor1, sensor2, sensor3], axis=-1)

final.shape

TensorShape([1134, 100, 400, 3])

现在我想做一个火车测试拆分

X_train, X_test, y_train, y_test = train_test_split(final, y, test_size=0.25, random_state=42, stratify=y)

但出现以下错误: 只有整数、切片 (:)、省略号 (...)、tf.newaxis (None) 和标量 tf.int32/tf.int64 张量是有效索引,得到数组([ 219 , 928, 636, 862, 606, 793, 621, 118, 1047, 635, .. 有任何想法吗?怎么了?

编辑: 当前仅使用 1 个传感器的工作解决方案。所以我认为问题在于三个 3 维度。

sensor1.shape
(100, 400, 1134)

final = np.moveaxis(sensor1, -1, 0)

final.shape
(1134, 100, 400)

X_train, X_test, y_train, y_test = train_test_split(final, y, test_size=0.25, random_state=42, stratify=y)

print("X:", final.shape)
print("y:", y.shape)

print("Xtrain:", X_train.shape)
print("y_train:", y_train.shape)
print("X_test:", X_test.shape)
print("y_test:", y_test.shape)

X: (1134, 100, 400)
y: (1134,)
Xtrain: (850, 100, 400)
y_train: (850,)
X_test: (284, 100, 400)
y_test: (284,)

X_train = tf.expand_dims(X_train, axis=-1)
X_test = tf.expand_dims(X_test, axis=-1)

print("X_train shape new:", X_train.shape)
X_train shape new: (850, 100, 400, 1)

【问题讨论】:

  • y的形状是什么?
  • y.shape = (1134,)
  • 使用 1 个传感器,使用 train_test_split 没问题,我使用与上面编辑过的帖子相同的 y。之后的 CNN 也运行良好..

标签: python tensorflow scikit-learn


【解决方案1】:

尝试使用 numpytrain_test_split 处理所有内容:

import tensorflow as tf
import numpy as np
from sklearn.model_selection import train_test_split

sensor1 = np.random.random((1134, 100, 400))
sensor2 = np.random.random((1134, 100, 400))
sensor3 = np.random.random((1134, 100, 400))
Y = np.random.random((1134))
final = np.stack([sensor1, sensor2, sensor3], axis=-1)

X_train, X_test, y_train, y_test = train_test_split(final, Y, test_size=0.25, random_state=42)

【讨论】:

  • 我将 tf.stack([sensor1, sensor2, sensor3], axis=-1) 更改为 np.stack([sensor1, sensor2, sensor3], axis=-1) 并且它有效!丹克!
猜你喜欢
  • 1970-01-01
  • 2022-01-09
  • 1970-01-01
  • 2022-08-19
  • 2021-09-03
  • 2016-04-29
  • 2017-12-08
  • 2019-03-13
  • 2017-11-24
相关资源
最近更新 更多