Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TensorFlow预训练神经网络的超参数整定_Python_Tensorflow_Keras_Hyperparameters - Fatal编程技术网

Python TensorFlow预训练神经网络的超参数整定

Python TensorFlow预训练神经网络的超参数整定,python,tensorflow,keras,hyperparameters,Python,Tensorflow,Keras,Hyperparameters,我开始学习Python,并尝试创建一个神经网络,用于检测和定位图像中的异常部分。我用的是TensorFlow的一个经过预训练的CNN。只要代码能够识别我的图像并对它们进行分类,它就可以工作。但是当我给他每堂课相同数量(大约100张)的图片时。准确率约为50%,这是随机的两类。所以我现在正在寻找一个解决方案。 我通过将图像分割成多个部分来解决定位问题。当每个图像的名称中都有其位置的参考时,异常被从非异常区域分割出来。因此,当图像被分类为某一类时,人们也会通过其名称了解其位置 第一个选择:我必须找到

我开始学习Python,并尝试创建一个神经网络,用于检测和定位图像中的异常部分。我用的是TensorFlow的一个经过预训练的CNN。只要代码能够识别我的图像并对它们进行分类,它就可以工作。但是当我给他每堂课相同数量(大约100张)的图片时。准确率约为50%,这是随机的两类。所以我现在正在寻找一个解决方案。 我通过将图像分割成多个部分来解决定位问题。当每个图像的名称中都有其位置的参考时,异常被从非异常区域分割出来。因此,当图像被分类为某一类时,人们也会通过其名称了解其位置

第一个选择:我必须找到一种方法来扩大我的图像数量,以提高我的准确性。我还没有做这方面的工作。 第二个选择:尝试调整CNN的超参数,也许把我的图像放在一些早期的层中。我看了几篇教程并试图实现它们,但它们都失败了,主要原因是数组的形状或不一致的数字


# Use scikit-learn to grid search the batch size and epochs
import numpy
from sklearn.model_selection import GridSearchCV
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
# Function to create model, required for KerasClassifier
def create_model():
    # create model
    model = Sequential()
    model.add(Dense(12, input_dim=8, activation='relu'))
    model.add(Dense(1, activation='sigmoid'))
    # Compile model
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model
# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
# load dataset
# create model
model = KerasClassifier(build_fn=create_model, verbose=0)
# define the grid search parameters
batch_size = [10, 20, 40, 60, 80, 100]
epochs = [10, 50, 100]
param_grid = dict(batch_size=batch_size, epochs=epochs)
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1)
grid_result = grid.fit(x_train, label_batch)
# summarize results
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
means = grid_result.cv_results_['mean_test_score']
stds = grid_result.cv_results_['std_test_score']
params = grid_result.cv_results_['params']
for mean, stdev, param in zip(means, stds, params):
    print("%f (%f) with: %r" % (mean, stdev, param))

init = tf.global_variables_initializer()
sess.run(init)

result = model.predict(image_batch)
result.shape

model.compile(
  optimizer=tf.train.AdamOptimizer(), 
  loss='categorical_crossentropy',
  metrics=['accuracy'])

class CollectBatchStats(tf.keras.callbacks.Callback):
  def __init__(self):
    self.batch_losses = []
    self.batch_acc = []

  def on_batch_end(self, batch, logs=None):
    self.batch_losses.append(logs['loss'])
    self.batch_acc.append(logs['acc'])

steps_per_epoch = image_data.samples//image_data.batch_size
batch_stats = CollectBatchStats()
model.fit((item for item in image_data), epochs=1, 
                    steps_per_epoch=steps_per_epoch,
                    callbacks = [batch_stats])

根据您在评论中提到的内容,您面临以下错误

ValueError:检查输入时出错:预期
密集输入
2维,但得到了具有形状的数组(212242243)

如果您使用的是CNN,为什么第一层是
密集层
(我从名称
密集输入
)而不是
卷积层

由于第一层是卷积层,您应该为参数传递
(224224,3)
input\u shape

使用CNN微调时装列表数据集的批次大小和年代数的完整代码如下所示:

# To support both python 2 and python 3
from __future__ import division, print_function, unicode_literals
from io import open

# Common imports
import numpy as np
import os
import tensorflow as tf
from keras.layers import Input, Conv2D, MaxPool2D, Dense, Dropout, Flatten
from keras.models import Sequential
from keras.optimizers import Adam
import matplotlib.pyplot as plt
from keras.regularizers import l1_l2
from matplotlib.pyplot import axis as ax
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import GridSearchCV


X = tf.placeholder(tf.float32, shape=[None, 784], name="X")
X_reshaped = tf.reshape(X, shape=[-1, 28, 28, 1])
y = tf.placeholder(tf.int32, shape=[None], name="y")

def create_model():

    # instantiate regularizer
    Regularizer = l1_l2(0.001)

    cnn_model = Sequential()

    cnn_model.add(Conv2D(filters = 64,kernel_size = 3, strides=(1, 1), input_shape = (28,28,1), 
                         activation='relu', data_format='channels_last', activity_regularizer=Regularizer))

    cnn_model.add(MaxPool2D(pool_size = (2, 2)))

    cnn_model.add(Dropout(0.25))

    cnn_model.add(Flatten())

    cnn_model.add(Dense(units = 32, activation = 'relu', activity_regularizer=Regularizer))

    cnn_model.add(Dense(units = 10, activation = 'sigmoid', activity_regularizer=Regularizer))

    cnn_model.compile(loss ='sparse_categorical_crossentropy', optimizer=Adam(lr=0.001),metrics =['accuracy'])
    return cnn_model

model = KerasClassifier(build_fn=create_model, verbose=0)

(X_train, y_train), (X_test, y_test) = tf.keras.datasets.fashion_mnist.load_data()
X_train = X_train.astype(np.float32).reshape(-1, 28*28) / 255.0
X_train_reshaped = np.reshape(X_train, newshape=[-1, 28, 28, 1])

X_test = X_test.astype(np.float32).reshape(-1, 28*28) / 255.0
X_test_reshaped = np.reshape(X_test, newshape=[-1, 28, 28, 1])

y_train = y_train.astype(int)
y_test = y_test.astype(int)


batch_size = [20, 40]
epochs = [10, 50]
param_grid = dict(batch_size=batch_size, epochs=epochs)
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=1, cv=3)
grid_result = grid.fit(X_train_reshaped, y_train)
# summarize results
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
means = grid_result.cv_results_['mean_test_score']
stds = grid_result.cv_results_['std_test_score']
params = grid_result.cv_results_['params']
for mean, stdev, param in zip(means, stds, params):
    print("%f (%f) with: %r" % (mean, stdev, param))

你的问题是什么?我读了你的整篇文章,但我不清楚你要求其他人帮助什么。我的模型无法解决这个问题,因为准确性不够好。因此,我正在考虑调整代码中的超参数,但我失败了。我正在寻找改进我的分类模型的技巧。我在网上找到的大多数东西都无法在代码中实现。e、 g.我尝试实施网格搜索,以找到合适的最佳历代数和批量大小:您的问题可能是因为您试图使用“二进制交叉熵”和度量值=”准确性“。如中所述,当使用“二进制交叉熵”时,您可能应该使用metrics=[categorical\u accurity]我对网格搜索有问题。它给了我:ValueError:检查输入时出错:预期密集输入有2维,但得到了形状为(212242243)的数组我给了他:grid\u result=grid.fit(x\u train,label\u batch)如代码所示我的x\u train是我的图像批次,label\u batch我加载了它们:找到了633个属于2个类的图像。图像批处理形状:(32,224,224,3)Labe批处理形状:(32,2)但他不能使用这些形状进行网格搜索。如何将它们转换为正确的形状。所以他给了我最好的参数