数据集 = 数据集[0:图像数量, :, :] 代码的作用是什么?

我在学习Udacity的深度学习课程时,遇到了一行代码我不明白它的作用和原因。能有人帮我理解一下吗?如果有人能分享相关的文档就更好了。

dataset = dataset[0:num_images, :, :]

来源:链接>load_letter 方法 – https://github.com/rndbrtrnd/udacity-deep-learning/blob/master/1_notmnist.ipynb

Load_letter 函数:

def load_letter(folder, min_num_images):    image_files = os.listdir(folder)    dataset = np.ndarray(shape=(len(image_files), image_size, image_size),                     dtype=np.float32)    image_index = 0    print(folder)    for image in os.listdir(folder):        image_file = os.path.join(folder, image)        try:            image_data = (ndimage.imread(image_file).astype(float) -                 pixel_depth / 2) / pixel_depth             if image_data.shape != (image_size, image_size):                raise Exception('Unexpected image shape: %s' % str(image_data.shape))             dataset[image_index, :, :] = image_data             image_index += 1        except IOError as e:              print('Could not read:', image_file, ':', e, '- it\'s ok, skipping.')    num_images = image_index    dataset = dataset[0:num_images, :, :]    if num_images < min_num_images:       raise Exception('Many fewer images than expected: %d < %d' %                (num_images, min_num_images))   print('Full dataset tensor:', dataset.shape)   print('Mean:', np.mean(dataset))   print('Standard deviation:', np.std(dataset))   return dataset

回答:

您的数据集中是一个张量(或多维数组)。在您的例子中,它有三个维度。

dataset[0:num_images, :, :]

在第一维度上,您选择了从0到图像数量的一组图像,而冒号’:’表示保留其余信息。

所以这只是在第一维度上进行张量过滤,而保持其余维度不变。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注