首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在keras_tuner中使用随机搜索

在keras_tuner中使用随机搜索
EN

Stack Overflow用户
提问于 2021-08-26 12:58:59
回答 1查看 697关注 0票数 0

当我运行RandomSearch.search时,我收到错误消息,即logits形状和标签形状是不同的。我不明白这是什么错误。有什么帮助吗?在错误消息之后:

tensorflow.python.framework.errors_impl.InvalidArgumentError:日志和标签必须具有相同的一维,得到逻辑形状32,15和标签形状480节点sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits (定义在ProgramData\Anaconda3\envs\tf\lib\site-packages\keras_tuner\engine\tuner.py:147)上) Op:__inference_train_function_828函数调用堆栈: train_function

以下是我的代码:

代码语言:javascript
复制
import tensorflow as tf
import keras_tuner as kt
from tensorflow import keras
from keras_tuner import RandomSearch
from keras_tuner.engine.hyperparameters import HyperParameters
import os
import cv2
import pandas as pd
import numpy as np
from tensorflow.keras.utils import to_categorical
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split


f= pd.read_csv('CELLS_ALL.csv')
Labels= f['labels']
path_dir = 'C:\\Users\\Tusneem\\PycharmProjects\\imageDataGen\\images\\'
img_all = []
for i in os.listdir(path_dir):
   img_sig = cv2.imread(path_dir+i)
   img_sig = cv2.resize(img_sig, (50, 50))
   img_all.append(img_sig)
x = np.array(img_all, dtype="float") / 255.0
y = Labels
le = LabelEncoder()
y = le.fit_transform(y)
y = to_categorical(y)
#print(labels)


(trainX, testX, trainY, testY) = train_test_split(x, y, test_size=0.25, random_state=42)

# for cnn images should me of shape (len(training,size,size, channel)

trainX= trainX.reshape(len(trainX),50,50,3)
testX = testX.reshape(len(testX),50,50,3)


def build_model(hp):
    model = keras.Sequential([
        keras.layers.Conv2D(
            filters=hp.Int('conv_1_filter', min_value=32, max_value=128, step=20),
            kernel_size=hp.Choice('conv_1_kernel', values=[3, 5]),
            activation='relu',
            input_shape=(50, 50, 3)
        ),
        keras.layers.Conv2D(
            filters=hp.Int('conv_2_filter', min_value=32, max_value=64, step=20),
            kernel_size=hp.Choice('conv_2_kernel', values=[3, 5]),
            activation='relu'
        ),
        keras.layers.Flatten(),
        keras.layers.Dense(
            units=hp.Int('dense_1_units', min_value=32, max_value=128, step=20),
            activation='relu'
        ),
        keras.layers.Dense(15, activation='softmax')
    ])

    model.compile(optimizer=keras.optimizers.Adam(hp.Choice('learning_rate', values=[1e-2, 1e-3])),
                  loss='sparse_categorical_crossentropy',
                  metrics=['accuracy'])

    return model
    model.compile(optimizer=keras.optimizers.Adam(hp.Choice('learning_rate', values=[1e-2, 1e-3])),
                  loss='sparse_categorical_crossentropy',
                  metrics=['accuracy'])
    model.summary()

    return model

tuner_search= kt.RandomSearch(build_model,
                          objective='val_accuracy',
                          max_trials=5) #,directory='tune',project_name="cnn model tunning"

tuner_search.search(trainX,trainY,epochs=5,validation_split=0.1)
model=tuner_search.get_best_models()[0]

问题就在这一行上:

tuner_search.search(trainX,trainY,epochs=5,validation_split=0.1)

我的问题是如何解决这个错误?

EN

回答 1

Stack Overflow用户

发布于 2021-08-26 13:30:55

我认为问题是不应该使用我的"y“变量作为热编码。它应该是表1到15,这是我的目标变量"y“中的类别数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68938984

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档