如何用keras对两组数据用相同的网络进行训练并且画在一个acc-loss图?

假如我有A,B两组数据,我想用两个的loss-acc图来对比得出哪组数据更好,所以如何将这两组数据同时进行训练并将结果画在一个acc-loss图?

1个回答

两组数据的训练的权重是否共享,如果是,用一个model,如果不是的话,可以用两个model,或者在model运行前,初始化下。

运行完得到 history 数据就是训练的acc,然后你得到两个 history以后用 pandas的zip合并,然后matplotlib.pyplot画图

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
关于keras 对模型进行训练 train_on_batch参数和模型输出的关系
在用keras+gym测试policy gradient进行小车杆平衡时模型搭建如下: ``` inputs = Input(shape=(4,),name='ob_inputs') x = Dense(16,activation='relu')(inputs) x = Dense(16,activation='relu')(x) x = Dense(1,activation='sigmoid')(x) model = Model(inputs=inputs,outputs = x) ``` 这里输出层是一个神经元,输出一个[0,1]之间的数,表示小车动作的概率 但是在代码训练过程中,模型的训练代码为: ``` X = np.array(states) y = np.array(list(zip(actions,discount_rewards))) loss = self.model.train_on_batch(X,y) ``` 这里的target data(y)是一个2维的列表数组,第一列是对应执行的动作,第二列是折扣奖励,那么在训练的时候,神经网络的输出数据和target data的维度不一致,是如何计算loss的呢?会自动去拟合y的第一列数据吗?
用keras搭建BP神经网络对数据集进行回归预测,效果和同学的相比很差,麻烦大神指点。新手小白。。。
数据集是csv文件,一共十三列,十几万行,第十三列是要预测的值。 试过很多种方法(都是百度的),包括更改网络层数、 节点数,学习率……,效果都没什么提升 不知道问题出在哪里,请大神指点。 import numpy as np import keras as ks from keras.models import Sequential from sklearn import preprocessing from sklearn.model_selection import train_test_split from keras.layers import Dense, Activation,Dropout x_yuan = np.loadtxt(open("shaixuandata.csv","rb"),\ usecols=(range(12)),delimiter=",",skiprows=1) x = preprocessing.scale(x_yuan) y = np.loadtxt(open("shaixuandata.csv","rb"),\ usecols=(12),delimiter=",",skiprows=1) x_train, x_test, y_train, y_test = train_test_split(\ x, y, test_size=0.25, random_state=43) model = Sequential() model.add(Dense(units=30, input_dim=12)) model.add(Activation('relu')) model.add(Dropout(0.1)) model.add(Dense(units=1)) model.add(Activation('linear')) ks.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.1, \ patience=10, verbose=0, mode='auto', epsilon=0.0001, cooldown=0, min_lr=0) sgd = ks.optimizers.SGD(lr=0.001, clipnorm=1.,decay=1e-6, momentum=0.9) model.compile(optimizer='sgd', loss='mae', metrics=['mae']) model.fit(x_train, y_train, batch_size=30, epochs=3, callbacks=None, \ validation_data=(x_test,y_test), shuffle=True, class_weight=None, \ sample_weight=None, initial_epoch=0) predict = model.predict(x_test) sum = 0 for i in range(len(y_test)): sum = sum+(y_test[i]-predict[i])**2 mse = sum/len(y_test) print(mse) ![训练的结果是这样的,老实说训练结果太差](https://img-ask.csdn.net/upload/201806/27/1530098555_142017.png)
tf.keras 关于 胶囊网络 capsule的问题
``` from tensorflow.keras import backend as K from tensorflow.keras.layers import Layer from tensorflow.keras import activations from tensorflow.keras import utils from tensorflow.keras.models import Model from tensorflow.keras.layers import * from tensorflow.keras.preprocessing.image import ImageDataGenerator from tensorflow.keras.callbacks import TensorBoard import mnist import tensorflow batch_size = 128 num_classes = 10 epochs = 20 """ 压缩函数,我们使用0.5替代hinton论文中的1,如果是1,所有的向量的范数都将被缩小。 如果是0.5,小于0.5的范数将缩小,大于0.5的将被放大 """ def squash(x, axis=-1): s_quared_norm = K.sum(K.square(x), axis, keepdims=True) + K.epsilon() scale = K.sqrt(s_quared_norm) / (0.5 + s_quared_norm) result = scale * x return result # 定义我们自己的softmax函数,而不是K.softmax.因为K.softmax不能指定轴 def softmax(x, axis=-1): ex = K.exp(x - K.max(x, axis=axis, keepdims=True)) result = ex / K.sum(ex, axis=axis, keepdims=True) return result # 定义边缘损失,输入y_true, p_pred,返回分数,传入即可fit时候即可 def margin_loss(y_true, y_pred): lamb, margin = 0.5, 0.1 result = K.sum(y_true * K.square(K.relu(1 - margin -y_pred)) + lamb * (1-y_true) * K.square(K.relu(y_pred - margin)), axis=-1) return result class Capsule(Layer): """编写自己的Keras层需要重写3个方法以及初始化方法 1.build(input_shape):这是你定义权重的地方。 这个方法必须设self.built = True,可以通过调用super([Layer], self).build()完成。 2.call(x):这里是编写层的功能逻辑的地方。 你只需要关注传入call的第一个参数:输入张量,除非你希望你的层支持masking。 3.compute_output_shape(input_shape): 如果你的层更改了输入张量的形状,你应该在这里定义形状变化的逻辑,这让Keras能够自动推断各层的形状。 4.初始化方法,你的神经层需要接受的参数 """ def __init__(self, num_capsule, dim_capsule, routings=3, share_weights=True, activation='squash', **kwargs): super(Capsule, self).__init__(**kwargs) # Capsule继承**kwargs参数 self.num_capsule = num_capsule self.dim_capsule = dim_capsule self.routings = routings self.share_weights = share_weights if activation == 'squash': self.activation = squash else: self.activation = activation.get(activation) # 得到激活函数 # 定义权重 def build(self, input_shape): input_dim_capsule = input_shape[-1] if self.share_weights: # 自定义权重 self.kernel = self.add_weight( name='capsule_kernel', shape=(1, input_dim_capsule, self.num_capsule * self.dim_capsule), initializer='glorot_uniform', trainable=True) else: input_num_capsule = input_shape[-2] self.kernel = self.add_weight( name='capsule_kernel', shape=(input_num_capsule, input_dim_capsule, self.num_capsule * self.dim_capsule), initializer='glorot_uniform', trainable=True) super(Capsule, self).build(input_shape) # 必须继承Layer的build方法 # 层的功能逻辑(核心) def call(self, inputs): if self.share_weights: hat_inputs = K.conv1d(inputs, self.kernel) else: hat_inputs = K.local_conv1d(inputs, self.kernel, [1], [1]) batch_size = K.shape(inputs)[0] input_num_capsule = K.shape(inputs)[1] hat_inputs = K.reshape(hat_inputs, (batch_size, input_num_capsule, self.num_capsule, self.dim_capsule)) hat_inputs = K.permute_dimensions(hat_inputs, (0, 2, 1, 3)) b = K.zeros_like(hat_inputs[:, :, :, 0]) for i in range(self.routings): c = softmax(b, 1) o = self.activation(K.batch_dot(c, hat_inputs, [2, 2])) if K.backend() == 'theano': o = K.sum(o, axis=1) if i < self.routings-1: b += K.batch_dot(o, hat_inputs, [2, 3]) if K.backend() == 'theano': o = K.sum(o, axis=1) return o def compute_output_shape(self, input_shape): # 自动推断shape return (None, self.num_capsule, self.dim_capsule) def MODEL(): input_image = Input(shape=(32, 32, 3)) x = Conv2D(64, (3, 3), activation='relu')(input_image) x = Conv2D(64, (3, 3), activation='relu')(x) x = AveragePooling2D((2, 2))(x) x = Conv2D(128, (3, 3), activation='relu')(x) x = Conv2D(128, (3, 3), activation='relu')(x) """ 现在我们将它转换为(batch_size, input_num_capsule, input_dim_capsule),然后连接一个胶囊神经层。模型的最后输出是10个维度为16的胶囊网络的长度 """ x = Reshape((-1, 128))(x) # (None, 100, 128) 相当于前一层胶囊(None, input_num, input_dim) capsule = Capsule(num_capsule=10, dim_capsule=16, routings=3, share_weights=True)(x) # capsule-(None,10, 16) output = Lambda(lambda z: K.sqrt(K.sum(K.square(z), axis=2)))(capsule) # 最后输出变成了10个概率值 model = Model(inputs=input_image, output=output) return model if __name__ == '__main__': # 加载数据 (x_train, y_train), (x_test, y_test) = mnist.load_data() x_train = x_train.astype('float32') x_test = x_test.astype('float32') x_train /= 255 x_test /= 255 y_train = tensorflow.keras.utils.to_categorical(y_train, num_classes) y_test = tensorflow.keras.utils.to_categorical(y_test, num_classes) # 加载模型 model = MODEL() model.compile(loss=margin_loss, optimizer='adam', metrics=['accuracy']) model.summary() tfck = TensorBoard(log_dir='capsule') # 训练 data_augmentation = True if not data_augmentation: print('Not using data augmentation.') model.fit( x_train, y_train, batch_size=batch_size, epochs=epochs, validation_data=(x_test, y_test), callbacks=[tfck], shuffle=True) else: print('Using real-time data augmentation.') # This will do preprocessing and realtime data augmentation: datagen = ImageDataGenerator( featurewise_center=False, # set input mean to 0 over the dataset samplewise_center=False, # set each sample mean to 0 featurewise_std_normalization=False, # divide inputs by dataset std samplewise_std_normalization=False, # divide each input by its std zca_whitening=False, # apply ZCA whitening rotation_range=0, # randomly rotate images in 0 to 180 degrees width_shift_range=0.1, # randomly shift images horizontally height_shift_range=0.1, # randomly shift images vertically horizontal_flip=True, # randomly flip images vertical_flip=False) # randomly flip images # Compute quantities required for feature-wise normalization # (std, mean, and principal components if ZCA whitening is applied). datagen.fit(x_train) # Fit the model on the batches generated by datagen.flow(). model.fit_generator( datagen.flow(x_train, y_train, batch_size=batch_size), epochs=epochs, validation_data=(x_test, y_test), callbacks=[tfck], workers=4) ``` 以上为代码 运行后出现该问题 ![图片说明](https://img-ask.csdn.net/upload/201902/26/1551184741_476774.png) ![图片说明](https://img-ask.csdn.net/upload/201902/26/1551184734_845838.png) 用官方的胶囊网络keras实现更改为tf下的keras实现仍出现该错误。
基于keras,使用imagedatagenerator.flow函数读入数据,训练集ACC极低
在做字符识别的神经网络,数据集是用序号标好名称的图片,标签取图片的文件名。想用Imagedatagenrator 函数和flow函数,增加样本的泛化性,然后生成数据传入网络,可是这样acc=1/类别数,基本为零。请问哪里出了问题 ``` datagen = ImageDataGenerator( width_shift_range=0.1, height_shift_range=0.1 ) def read_train_image(self, name): myimg = Image.open(name).convert('RGB') return np.array(myimg) def train(self): #训练集 train_img_list = [] train_label_list = [] #测试集 test_img_list = [] test_label_list = [] for file in os.listdir('train'): files_img_in_array = self.read_train_image(name='train/' + file) train_img_list.append(files_img_in_array) # Image list add up train_label_list.append(int(file.split('_')[0])) # lable list addup for file in os.listdir('test'): files_img_in_array = self.read_train_image(name='test/' + file) test_img_list.append(files_img_in_array) # Image list add up test_label_list.append(int(file.split('_')[0])) # lable list addup train_img_list = np.array(train_img_list) train_label_list = np.array(train_label_list) test_img_list = np.array(train_img_list) test_label_list = np.array(train_label_list) train_label_list = np_utils.to_categorical(train_label_list, 5788) test_label_list = np_utils.to_categorical(test_label_list, 5788) train_img_list = train_img_list.astype('float32') test_img_list = test_img_list.astype('float32') test_img_list /= 255.0 train_img_list /= 255.0 ``` 这是图片数据的处理,图片和标签都存到list里。下面是用fit_genrator训练 ``` model.fit_generator( self.datagen.flow(x=train_img_list, y=train_label_list, batch_size=2), samples_per_epoch=len(train_img_list), epochs=10, validation_data=(test_img_list,test_label_list), ) ```
keras实现人脸识别,训练失败……请教大神指点迷津!!!
![图片说明](https://img-ask.csdn.net/upload/201904/26/1556209614_615215.jpg) 各位大神,如图所示,在训练过程中,第二轮开始出现问题,这是什么原因呢? 代码如下: ------------------------------------------------- ``` import random import keras import numpy as np import cv2 from sklearn.model_selection import train_test_split from keras.preprocessing.image import ImageDataGenerator from keras.models import Sequential from keras.layers import Dense, Dropout, Activation, Flatten from keras.layers import Convolution2D, MaxPooling2D from keras.optimizers import SGD from keras.utils import np_utils from keras.models import load_model from keras import backend as K from source_data import load_dataset,resize_img #定义数据集格式 class Dataset: def __init__(self, path_name): #训练数据集 self.train_images = None self.train_labels = None #测试集 self.valid_images = None self.valid_labels = None #样本数据 self.test_images = None self.test_labels = None #load路径 self.path_name = path_name #维度顺序 self.input_shape = None #加载数据集并按照交叉验证的原则划分数据集,完成数据预处理 def load(self,img_rows=64, img_cols=64,img_channels = 3,nb_classes = 2): #加载数据集到内存 images,labels=load_dataset(self.path_name)#函数调用 train_images, valid_images, train_labels, valid_labels= train_test_split(images, labels, test_size = 0.3, random_state = random.randint(0, 100)) _, test_images, _, test_labels = train_test_split(images, labels, test_size = 0.5, random_state = random.randint(0, 100)) #根据backend类型确定输入图片数据时的顺序为:channels,rows,cols,否则:rows,cols,channels #这部分代码就是根据keras库要求的维度顺序重组训练数据集 train_images = train_images.reshape(train_images.shape[0], img_rows, img_cols, img_channels) valid_images = valid_images.reshape(valid_images.shape[0], img_rows, img_cols, img_channels) test_images = test_images.reshape(test_images.shape[0], img_rows, img_cols, img_channels) self.input_shape = (img_rows, img_cols, img_channels) #输出训练集、验证集、测试集的数量 print(train_images.shape[0], 'train samples') print(valid_images.shape[0], 'valid samples') print(test_images.shape[0], 'test samples') #我们的模型使用categorical_crossentropy作为损失函数,因此需要根据类别数量nb_classes将 #类别标签进行one-hot编码使其向量化,在这里我们的类别只有两种,经过转化后标签数据变为二维 train_labels = np_utils.to_categorical(train_labels, nb_classes) valid_labels = np_utils.to_categorical(valid_labels, nb_classes) test_labels = np_utils.to_categorical(test_labels, nb_classes) #像素数据浮点化以便归一化 train_images = train_images.astype('float32') valid_images = valid_images.astype('float32') test_images = test_images.astype('float32') #将其归一化,图像的各像素值归一化到0—1区间 train_images /= 255 valid_images /= 255 test_images /= 255 self.train_images = train_images self.valid_images = valid_images self.test_images = test_images self.train_labels = train_labels self.valid_labels = valid_labels self.test_labels = test_labels class Model: def __init__(self): self.model = None #建立keras模型 def build_model(self, dataset, nb_classes = 2): #构建一个空的网络模型,序贯模型或线性堆叠模型,添加各个layer self.model = Sequential() #以下代码将顺序添加CNN网络需要的各层,一个add就是一个网络层 self.model.add(Convolution2D(32, 3, 3, border_mode='same', input_shape = dataset.input_shape)) #1 2维卷积层 self.model.add(Activation('relu')) #2 激活函数层 self.model.add(Convolution2D(32, 3, 3)) #3 2维卷积层 self.model.add(Activation('relu')) #4 激活函数层 self.model.add(MaxPooling2D(pool_size=(2, 2))) #5 池化层 self.model.add(Dropout(0.25)) #6 Dropout层 self.model.add(Convolution2D(64, 3, 3, border_mode='same')) #7 2维卷积层 self.model.add(Activation('relu')) #8 激活函数层 self.model.add(Convolution2D(64, 3, 3)) #9 2维卷积层 self.model.add(Activation('relu')) #10 激活函数层 self.model.add(MaxPooling2D(pool_size=(2, 2))) #11 池化层 self.model.add(Dropout(0.25)) #12 Dropout层 self.model.add(Flatten()) #13 Flatten层 self.model.add(Dense(512)) #14 Dense层,又被称作全连接层 self.model.add(Activation('relu')) #15 激活函数层 self.model.add(Dropout(0.5)) #16 Dropout层 self.model.add(Dense(nb_classes)) #17 Dense层 self.model.add(Activation('softmax')) #18 分类层,输出最终结果 #Prints a string summary of the network self.model.summary() #训练模型 def train(self, dataset, batch_size = 20, nb_epoch = 10, data_augmentation = True): sgd = SGD(lr = 0.01, decay = 1e-6, momentum = 0.9, nesterov = True) #采用随机梯度下降优化器进行训练,首先生成一个优化器对象 self.model.compile(loss='categorical_crossentropy', optimizer=sgd,metrics=['accuracy']) #完成实际的模型配置 #不使用数据提升,所谓的提升就是从我们提供的训练数据中利用旋转、翻转、加噪声等方法提升训练数据规模,增加模型训练量 if not data_augmentation: self.model.fit(dataset.train_images, dataset.train_labels, batch_size = batch_size, epochs = nb_epoch, validation_data = (dataset.valid_images, dataset.valid_labels), shuffle = True) #使用实时数据提升 else: #定义数据生成器用于数据提升,其返回一个生成器对象datagen,datagen每被调用一 #次其生成一组数据(顺序生成),节省内存,其实就是python的数据生成器 datagen = ImageDataGenerator( featurewise_center = False, #是否使输入数据去中心化(均值为0), samplewise_center = False, #是否使输入数据的每个样本均值为0 featurewise_std_normalization = False, #是否数据标准化(输入数据除以数据集的标准差) samplewise_std_normalization = False, #是否将每个样本数据除以自身的标准差 zca_whitening = False, #是否对输入数据施以ZCA白化 rotation_range = 20, #数据提升时图片随机转动的角度(范围为0~180) width_shift_range = 0.2, #数据提升时图片水平偏移的幅度(单位为图片宽度的占比,0~1之间的浮点数) height_shift_range = 0.2, #同上,只不过这里是垂直 horizontal_flip = True, #是否进行随机水平翻转 vertical_flip = False) #是否进行随机垂直翻转 #计算整个训练样本集的数量以用于特征值归一化等处理 datagen.fit(dataset.train_images) #利用生成器开始训练模型—0.7*N self.model.fit_generator(datagen.flow(dataset.train_images, dataset.train_labels, batch_size = batch_size), steps_per_epoch = dataset.train_images.shape[0], epochs = nb_epoch, validation_data = (dataset.valid_images, dataset.valid_labels)) if __name__ == '__main__': dataset = Dataset('e:\saving') dataset.load()#实例操作,完成实际数据加载和预处理 model = Model() model.build_model(dataset) #训练数据 model.train(dataset) ```
keras model.fit_generator训练完一个epoch之后无法加载训练集怎么处理?
1、在训练神经网络的过程中遇到了训练完一个epoch之后无法继续训练的问题,具体问题截图如下 ![图片说明](https://img-ask.csdn.net/upload/202002/08/1581151633_972155.png) 数据生成的代码如下 ``` def GET_DATASET_SHUFFLE(train_x, train_y, batch_size): #random.shuffle(X_samples) batch_num = int(len(train_x) / batch_size) max_len = batch_num * batch_size X_samples = np.array(train_x[0:max_len]) Y_samples = np.array(train_y[0:max_len]) X_batches = np.split(X_samples, batch_num) Y_batches = np.split(Y_samples, batch_num) for i in range(batch_num): x = np.array(list(map(load_image, X_batches[i]))) y = np.array(list(map(load_label, Y_batches[i]))) yield x, y ``` 想要向各位大神请教一下,刚刚接触这个不是太懂
keras模型的预测(predict)结果全是0
使用keras搭了一个模型并且对其进行了训练,得到模型在百度云盘中:链接:https://pan.baidu.com/s/1wQ5MLhPDfhwlveY-ib92Ew 密码:f3gk, 使用keras.predict时,无论模型输入什么输出都是0,代码如下: ```python from keras.models import Sequential, Model from keras.layers.convolutional_recurrent import ConvLSTM2D from keras.layers.normalization import BatchNormalization from keras.utils import plot_model from keras.models import load_model from keras import metrics import numpy as np import os import json import keras import matplotlib.pyplot as plt import math from keras import losses import shutil from keras import backend as K from keras import optimizers # 定义损失函数 def my_loss(y_true, y_pred): if not K.is_tensor(y_pred): y_pred = K.constant(y_pred, dtype = 'float64') y_true = K.cast(y_true, y_pred.dtype) return K.mean(K.abs((y_true - y_pred) / K.clip(K.abs(y_true), K.epsilon(), None))) # 定义评价函数metrics def mean_squared_percentage_error(y_true, y_pred): if not K.is_tensor(y_pred): y_pred = K.constant(y_pred, dtype = 'float64') y_true = K.cast(y_true, y_pred.dtype) return K.mean(K.square((y_pred - y_true)/K.clip(K.abs(y_true),K.epsilon(), None))) model_path = os.path.join('model/model' ,'model.h5') seq = load_model(model_path, custom_objects={'my_loss': my_loss,'mean_squared_percentage_error':mean_squared_percentage_error}) print (seq.summary()) input_data = np.random.random([1, 12, 56, 56, 1]) output_data = seq.predict(input_data, batch_size=16, verbose=1) print (output_data[0][:,:,0]) ``` 输出如下: ```python Model: "sequential_1" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv_lst_m2d_1 (ConvLSTM2D) (None, None, 56, 56, 40) 59200 _________________________________________________________________ batch_normalization_1 (Batch (None, None, 56, 56, 40) 160 _________________________________________________________________ conv_lst_m2d_2 (ConvLSTM2D) (None, None, 56, 56, 40) 115360 _________________________________________________________________ batch_normalization_2 (Batch (None, None, 56, 56, 40) 160 _________________________________________________________________ conv_lst_m2d_3 (ConvLSTM2D) (None, 56, 56, 1) 1480 ================================================================= Total params: 176,360 Trainable params: 176,200 Non-trainable params: 160 None 1/1 [==============================] - 1s 812ms/step [[ 0. 0. 0. ... 0. 0. 0.] [ 0. 0. 0. ... 0. 0. 0.] [ 0. 0. 0. ... 0. 0. 0.] ... [ 0. 0. 0. ... 0. 0. 0.] [ 0. 0. 0. ... 0. 0. 0.] [ 0. 0. 0. ... 0. 0. -0.]] ``` 不懂为什么会这样,即便随机生成一组数据作为输入,结果也是这样
使用Keras找不到tensorflow
程序代码 #-*- coding: utf-8 -*- #使用神经网络算法预测销量高低 import pandas as pd #参数初始化 inputfile = 'D:/python/chapter5/demo/data/sales_data.xls' data = pd.read_excel(inputfile, index_col = u'序号') #导入数据 #数据是类别标签,要将它转换为数据 #用1来表示“好”、“是”、“高”这三个属性,用0来表示“坏”、“否”、“低” data[data == u'好'] = 1 data[data == u'是'] = 1 data[data == u'高'] = 1 data[data != 1] = 0 x = data.iloc[:,:3].as_matrix().astype(int) y = data.iloc[:,3].as_matrix().astype(int) from keras.models import Sequential from keras.layers.core import Dense, Activation model = Sequential() #建立模型 model.add(Dense(input_dim = 3, output_dim = 10)) model.add(Activation('relu')) #用relu函数作为激活函数,能够大幅提供准确度 model.add(Dense(input_dim = 10, output_dim = 1)) model.add(Activation('sigmoid')) #由于是0-1输出,用sigmoid函数作为激活函数 model.compile(loss = 'binary_crossentropy', optimizer = 'adam', class_mode = 'binary') #编译模型。由于我们做的是二元分类,所以我们指定损失函数为binary_crossentropy,以及模式为binary #另外常见的损失函数还有mean_squared_error、categorical_crossentropy等,请阅读帮助文件。 #求解方法我们指定用adam,还有sgd、rmsprop等可选 model.fit(x, y, nb_epoch = 1000, batch_size = 10) #训练模型,学习一千次 yp = model.predict_classes(x).reshape(len(y)) #分类预测 from cm_plot import * #导入自行编写的混淆矩阵可视化函数 cm_plot(y,yp).show() #显示混淆矩阵可视化结果 错误提示 Using TensorFlow backend. Traceback (most recent call last): File "D:\python\chapter5\demo\code\5-3_neural_network.py", line 19, in <module> from keras.models import Sequential File "C:\Python27\lib\site-packages\keras\__init__.py", line 3, in <module> from . import utils File "C:\Python27\lib\site-packages\keras\utils\__init__.py", line 6, in <module> from . import conv_utils File "C:\Python27\lib\site-packages\keras\utils\conv_utils.py", line 3, in <module> from .. import backend as K File "C:\Python27\lib\site-packages\keras\backend\__init__.py", line 83, in <module> from .tensorflow_backend import * File "C:\Python27\lib\site-packages\keras\backend\tensorflow_backend.py", line 1, in <module> import tensorflow as tf ImportError: No module named tensorflow
关于 keras 中用ImageDataGenerator 做 data augmentation 的问题
各位大神好,小白刚接触深度学习和keras. 有两个问题一直困扰着我,用keras中的 ImageDataGenerator做data augmentation时, (1)每个epoch的图片都不同,这样的做,反向传播时修改的参数还准确吗,训练模型严谨吗, (2)我试着输出过训练图像,发现里面没有原始图像,这样做数据扩张感觉很慌,是不是我使用方法的问题啊,请大佬指点迷津 ``` datagen = ImageDataGenerator( rescale=None, shear_range=0.2, zoom_range=[0.95,1.05], rotation_range=10, horizontal_flip=True, vertical_flip=True, fill_mode='reflect', ) training = model.fit_generator(datagen.flow(data_train, label_train_binary, batch_size=n_batch, shuffle=True), callbacks=[checkpoint,tensorboard,csvlog],validation_data=(data_val,label_val_binary),steps_per_epoch=len(data_train)//n_batch, nb_epoch=10000, verbose=1) ```
迁移学习中进行医学影像分析,训练神经网络后accuracy保持不变。。。
使用的是vgg16的finetune网络,网络权重从keras中导入的,最上层有三层的一个小训练器的权重是由训练习得的。训练集大约300个样本,验证集大约80个样本,但程序运行后,第一二个epoch之间loss、acc还有变化,之后就不再变化,而且验证集的准确度一直接近于零。。想请有关卷积神经网络和机器学习方面的大神帮忙看一下是哪里出了问题 import keras from keras.models import Sequential from keras.layers import Dense,Dropout,Activation,Flatten from keras.layers import GlobalAveragePooling2D import numpy as np from keras.optimizers import RMSprop from keras.utils import np_utils import matplotlib.pyplot as plt from keras import regularizers from keras.applications.vgg16 import VGG16 from keras import optimizers from keras.layers.core import Lambda from keras import backend as K from keras.models import Model #写一个LossHistory类(回调函数),保存loss和acc,在keras下画图 class LossHistory(keras.callbacks.Callback): def on_train_begin(self, logs={}):#在每个batch的开始处(on_batch_begin):logs包含size,即当前batch的样本数 self.losses = {'batch':[], 'epoch':[]} self.accuracy = {'batch':[], 'epoch':[]} self.val_loss = {'batch':[], 'epoch':[]} self.val_acc = {'batch':[], 'epoch':[]} def on_batch_end(self, batch, logs={}): self.losses['batch'].append(logs.get('loss')) self.accuracy['batch'].append(logs.get('acc')) self.val_loss['batch'].append(logs.get('val_loss')) self.val_acc['batch'].append(logs.get('val_acc')) def on_epoch_end(self, batch, logs={}):#每迭代完一次从log中取得数据 self.losses['epoch'].append(logs.get('loss')) self.accuracy['epoch'].append(logs.get('acc')) self.val_loss['epoch'].append(logs.get('val_loss')) self.val_acc['epoch'].append(logs.get('val_acc')) def loss_plot(self, loss_type): iters = range(len(self.losses[loss_type])) #绘图的横坐标? plt.figure() #建立一个空的画布 if loss_type == 'epoch': plt.subplot(211) plt.plot(iters,self.accuracy[loss_type],'r',label='train acc') plt.plot(iters,self.val_acc[loss_type],'b',label='val acc') # val_acc用蓝色线表示 plt.grid(True) plt.xlabel(loss_type) plt.ylabel('accuracy') plt.show() plt.subplot(212) plt.plot(iters, self.losses[loss_type], 'r', label='train loss') # val_acc 用蓝色线表示 plt.plot(iters, self.val_loss[loss_type], 'b', label='val loss') # val_loss 用黑色线表示 plt.xlabel(loss_type) plt.ylabel('loss') plt.legend(loc="upper right") #把多个axs的图例放在一张图上,loc表示位置 plt.show() print(np.mean(self.val_acc[loss_type])) print(np.std(self.val_acc[loss_type])) seed = 7 np.random.seed(seed) #训练网络的几个参数 batch_size=32 num_classes=2 epochs=100 weight_decay=0.0005 learn_rate=0.0001 #读入训练、测试数据,改变大小,显示基本信息 X_train=np.load(open('/image_BRATS_240_240_3_normal.npy',mode='rb')) Y_train=np.load(open('/label_BRATS_240_240_3_normal.npy',mode='rb')) Y_train = keras.utils.to_categorical(Y_train, 2) #搭建神经网络 model_vgg16=VGG16(include_top=False,weights='imagenet',input_shape=(240,240,3),classes=2) model_vgg16.layers.pop() model=Sequential() model.add(model_vgg16) model.add(Flatten(input_shape=X_train.shape[1:])) model.add(Dense(436,activation='relu')) #return x*10的向量 model.add(Dense(2,activation='softmax')) #model(inputs=model_vgg16.input,outputs=predictions) for layer in model_vgg16.layers[:13]: layer.trainable=False model_vgg16.summary() model.compile(optimizer=RMSprop(lr=learn_rate,decay=weight_decay), loss='categorical_crossentropy', metrics=['accuracy']) model.summary() history=LossHistory() model.fit(X_train,Y_train, batch_size=batch_size,epochs=epochs, verbose=1, shuffle=True, validation_split=0.2, callbacks=[history]) #模型评估 history.loss_plot('epoch') 比如: ![实验运行结果:](https://img-ask.csdn.net/upload/201804/19/1524134477_869793.png)
使用keras搭建黑体汉字单个字符识别网络val_acc=0.0002
这是我读入训练数据的过程,数据集是根据txt文件生成的单个汉字的图像(shape为64*64)788种字符(包括数字和X字符),每张图片的开头命名为在txt字典中的位置,作为标签, ``` def read_train_image(self, name): img = Image.open(name).convert('RGB') return np.array(img) def train(self): train_img_list = [] train_label_list = [] for file in os.listdir('train'): files_img_in_array = self.read_train_image(name='train/'+ file) train_img_list.append(files_img_in_array) # Image list add up train_label_list.append(int(file.split('_')[0])) # lable list addup train_img_list = np.array(train_img_list) train_label_list = np.array(train_label_list) train_label_list = np_utils.to_categorical(train_label_list, self.count) train_img_list = train_img_list.astype('float32') train_img_list /= 255 ``` 训练下来,虽然train_acc达到0.99,但是验证accuracy一直都等于0. 下面是网络结构: ``` model = Sequential() #创建第一个卷积层。 model.add(Convolution2D(32, 3, 3, border_mode='valid', input_shape=(64,64,3),kernel_regularizer=l2(0.0001))) model.add(BatchNormalization(axis=3)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) #创建第二个卷积层。 model.add(Convolution2D(64, 3, 3, border_mode='valid',kernel_regularizer=l2(0.0001))) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) #创建第三个卷积层。 model.add(Convolution2D(128, 3, 3, border_mode='valid',kernel_regularizer=l2(0.0001))) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) # 创建全连接层。 model.add(Flatten()) model.add(Dense(128, init= 'he_normal')) model.add(BatchNormalization()) model.add(Activation('relu')) #创建输出层,使用 Softmax函数输出属于各个字符的概率值。 model.add(Dense(output_dim=self.count, init= 'he_normal')) model.add(Activation('softmax')) #设置神经网络中的损失函数和优化算法。 model.compile(loss='categorical_crossentropy', optimizer='adam',metrics=['accuracy']) #开始训练,并设置批尺寸和训练的步数。 model.fit( train_img_list, train_label_list, epochs=500, batch_size=128, validation_split=0.2, verbose=1, shuffle= False, ) ``` 大概结构是这样,十几轮后训练集acc达到了0.99.验证集val_acc为0.网上说这种情况大概是过拟合了,希望高手指点一下。
mlp 如何加载 doc2vec( .d2c)模型数据进行训练
mlp模型如下: ``` def MySimpleMLP(feature=700, vec_size=50): auc_roc = LSTM.as_keras_metric(tf.compat.v1.metrics.auc) model = Sequential() model.add(Flatten()) model.add(Dense(32, activation='relu', input_shape=(52,))) model.add(Dropout(0.2)) model.add(Dense(32, activation='relu')) model.add(Dropout(0.2)) model.add(Dense(1, activation='softmax')) # compile model model.compile(loss="binary_crossentropy", optimizer="adam", metrics=[auc_roc]) return model ``` 训练函数如下: ``` model.fit(trainData, trainLabel, validation_split=0.2, epochs=10, batch_size=64, verbose=2) ``` do2vec模型是基于 imdb_50.d2v。 跪求各位大佬。
keras下self-attention和Recall, F1-socre值实现问题?
麻烦大神帮忙看一下: (1)为何返回不了Precise, Recall, F1-socre值? (2)为何在CNN前加了self-attention层,训练后的acc反而降低在0.78上下? 【研一小白求详解,万分感谢大神】 ``` import os #导入os模块,用于确认文件是否存在 import numpy as np from keras.preprocessing.text import Tokenizer from keras.preprocessing.sequence import pad_sequences from keras.callbacks import Callback from sklearn.metrics import f1_score, precision_score, recall_score maxlen = 380#句子长截断为100 training_samples = 20000#在 200 个样本上训练 validation_samples = 5000#在 10 000 个样本上验证 max_words = 10000#只考虑数据集中前 10 000 个最常见的单词 def dataProcess(): imdb_dir = 'data/aclImdb'#基本路径,经常要打开这个 #处理训练集 train_dir = os.path.join(imdb_dir, 'train')#添加子路径 train_labels = [] train_texts = [] for label_type in ['neg', 'pos']: dir_name = os.path.join(train_dir, label_type) for fname in os.listdir(dir_name):#获取目录下所有文件名字 if fname[-4:] == '.txt': f = open(os.path.join(dir_name, fname),'r',encoding='utf8') train_texts.append(f.read()) f.close() if label_type == 'neg': train_labels.append(0) else:train_labels.append(1) #处理测试集 test_dir = os.path.join(imdb_dir, 'test') test_labels = [] test_texts = [] for label_type in ['neg', 'pos']: dir_name = os.path.join(test_dir, label_type) for fname in sorted(os.listdir(dir_name)): if fname[-4:] == '.txt': f = open(os.path.join(dir_name, fname),'r',encoding='utf8') test_texts.append(f.read()) f.close() if label_type == 'neg': test_labels.append(0) else: test_labels.append(1) #对数据进行分词和划分训练集和数据集 tokenizer = Tokenizer(num_words=max_words) tokenizer.fit_on_texts(train_texts)#构建单词索引结构 sequences = tokenizer.texts_to_sequences(train_texts)#整数索引的向量化模型 word_index = tokenizer.word_index#索引字典 print('Found %s unique tokens.' % len(word_index)) data = pad_sequences(sequences, maxlen=maxlen) train_labels = np.asarray(train_labels)#把列表转化为数组 print('Shape of data tensor:', data.shape) print('Shape of label tensor:', train_labels.shape) indices = np.arange(data.shape[0])#评论顺序0,1,2,3 np.random.shuffle(indices)#把评论顺序打乱3,1,2,0 data = data[indices] train_labels = train_labels[indices] x_train = data[:training_samples] y_train = train_labels[:training_samples] x_val = data[training_samples: training_samples + validation_samples] y_val = train_labels[training_samples: training_samples + validation_samples] #同样需要将测试集向量化 test_sequences = tokenizer.texts_to_sequences(test_texts) x_test = pad_sequences(test_sequences, maxlen=maxlen) y_test = np.asarray(test_labels) return x_train,y_train,x_val,y_val,x_test,y_test,word_index embedding_dim = 100#特征数设为100 #"""将预训练的glove词嵌入文件,构建成可以加载到embedding层中的嵌入矩阵""" def load_glove(word_index):#导入glove的词向量 embedding_file='data/glove.6B' embeddings_index={}#定义字典 f = open(os.path.join(embedding_file, 'glove.6B.100d.txt'),'r',encoding='utf8') for line in f: values = line.split() word = values[0] coefs = np.asarray(values[1:], dtype='float32') embeddings_index[word] = coefs f.close() # """转化为矩阵:构建可以加载到embedding层中的嵌入矩阵,形为(max_words(单词数), embedding_dim(向量维数)) """ embedding_matrix = np.zeros((max_words, embedding_dim)) for word, i in word_index.items():#字典里面的单词和索引 if i >= max_words:continue embedding_vector = embeddings_index.get(word) if embedding_vector is not None: embedding_matrix[i] = embedding_vector return embedding_matrix if __name__ == '__main__': x_train, y_train, x_val, y_val,x_test,y_test, word_index = dataProcess() embedding_matrix=load_glove(word_index) #可以把得到的嵌入矩阵保存起来,方便后面fine-tune""" # #保存 from keras.models import Sequential from keras.layers.core import Dense,Dropout,Activation,Flatten from keras.layers.recurrent import LSTM from keras.layers import Embedding from keras.layers import Bidirectional from keras.layers import Conv1D, MaxPooling1D import keras from keras_self_attention import SeqSelfAttention model = Sequential() model.add(Embedding(max_words, embedding_dim, input_length=maxlen)) model.add(SeqSelfAttention(attention_activation='sigmod')) model.add(Conv1D(filters = 64, kernel_size = 5, padding = 'same', activation = 'relu')) model.add(MaxPooling1D(pool_size = 4)) model.add(Dropout(0.25)) model.add(Bidirectional(LSTM(64,activation='tanh',dropout=0.2,recurrent_dropout=0.2))) model.add(Dense(256, activation='relu')) model.add(Dropout(0.2)) model.add(Dense(1, activation='sigmoid')) model.summary() model.layers[0].set_weights([embedding_matrix]) model.layers[0].trainable = False model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['acc']) class Metrics(Callback): def on_train_begin(self, logs={}): self.val_f1s = [] self.val_recalls = [] self.val_precisions = [] def on_epoch_end(self, epoch, logs={}): val_predict = (np.asarray(self.model.predict(self.validation_data[0]))).round() val_targ = self.validation_data[1] _val_f1 = f1_score(val_targ, val_predict) _val_recall = recall_score(val_targ, val_predict) _val_precision = precision_score(val_targ, val_predict) self.val_f1s.append(_val_f1) self.val_recalls.append(_val_recall) self.val_precisions.append(_val_precision) return metrics = Metrics() history = model.fit(x_train, y_train, epochs=10, batch_size=32, validation_data=(x_val, y_val), callbacks=[metrics]) model.save_weights('pre_trained_glove_model.h5')#保存结果 ```
用神经网络训练模型,报错字符串不能转换为浮点,请问怎么解决?
import matplotlib.pyplot as plt from math import sqrt from matplotlib import pyplot import pandas as pd from numpy import concatenate from sklearn.preprocessing import MinMaxScaler from sklearn.metrics import mean_squared_error from keras.models import Sequential from keras.layers.core import Dense, Dropout, Activation from keras.optimizers import Adam import tensorflow ''' keras实现神经网络回归模型 ''' # 读取数据 path = 'data001.csv' # 删掉不用字符串字段 train = pd.read_csv(path) dataset = train.iloc[1:,:] # df转array values = dataset.values # 原始数据标准化,为了加速收敛 scaler = MinMaxScaler(feature_range=(0, 1)) scaled = scaler.fit_transform(values) y = scaled[:, -1] X = scaled[:, 0:-1] # 随机拆分训练集与测试集 from sklearn.model_selection import train_test_split train_X, test_X, train_y, test_y = train_test_split(X, y, test_size=0.3) # 全连接神经网络 model = Sequential() input = X.shape[1] # 隐藏层128 model.add(Dense(128, input_shape=(input,))) model.add(Activation('relu')) # Dropout层用于防止过拟合 # model.add(Dropout(0.2)) # 隐藏层128 model.add(Dense(128)) model.add(Activation('relu')) # model.add(Dropout(0.2)) # 没有激活函数用于输出层,因为这是一个回归问题,我们希望直接预测数值,而不需要采用激活函数进行变换。 model.add(Dense(1)) # 使用高效的 ADAM 优化算法以及优化的最小均方误差损失函数 model.compile(loss='mean_squared_error', optimizer=Adam()) # early stoppping from keras.callbacks import EarlyStopping early_stopping = EarlyStopping(monitor='val_loss', patience=50, verbose=2) # 训练 history = model.fit(train_X, train_y, epochs=300, batch_size=20, validation_data=(test_X, test_y), verbose=2, shuffle=False, callbacks=[early_stopping]) # loss曲线 pyplot.plot(history.history['loss'], label='train') pyplot.plot(history.history['val_loss'], label='test') pyplot.legend() pyplot.show() # 预测 yhat = model.predict(test_X) # 预测y逆标准化 inv_yhat0 = concatenate((test_X, yhat), axis=1) inv_yhat1 = scaler.inverse_transform(inv_yhat0) inv_yhat = inv_yhat1[:, -1] # 原始y逆标准化 test_y = test_y.reshape((len(test_y), 1)) inv_y0 = concatenate((test_X, test_y), axis=1) inv_y1 = scaler.inverse_transform(inv_y0) inv_y = inv_y1[:, -1] # 计算 RMSE rmse = sqrt(mean_squared_error(inv_y, inv_yhat)) print('Test RMSE: %.3f' % rmse) plt.plot(inv_y) plt.plot(inv_yhat) plt.show() ``` ``` 报错是:Traceback (most recent call last): File "F:/SSD/CNN.py", line 24, in <module> scaled = scaler.fit_transform(values) File "D:\anaconda\lib\site-packages\sklearn\base.py", line 464, in fit_transform return self.fit(X, **fit_params).transform(X) File "D:\anaconda\lib\site-packages\sklearn\preprocessing\data.py", line 334, in fit return self.partial_fit(X, y) File "D:\anaconda\lib\site-packages\sklearn\preprocessing\data.py", line 362, in partial_fit force_all_finite="allow-nan") File "D:\anaconda\lib\site-packages\sklearn\utils\validation.py", line 527, in check_array array = np.asarray(array, dtype=dtype, order=order) File "D:\anaconda\lib\site-packages\numpy\core\numeric.py", line 538, in asarray return array(a, dtype, copy=False, order=order) ValueError: could not convert string to float: 'label' label是csv文件里的列名,但是就算去掉,还是会报这个错误
python如何自定义权重损失函数?
我利用keras的神经网络训练一个模型,被训练的数据是一个很大的二维数组,每一行是一个类别,总共有3种类别。被训练出的模型中包括3种类别(暂且称为A,B,C)。现在B类的预测准确率太高了,而A和C类的预测准确率较低,我想在把B类准确率适当减低的情况下来提高A和C类的预测准确率。请问该怎么操作? 代码如下,我从网上查了一些代码,自己不是特别明白,尝试后,出现了错误。请问该如何修改?下面添加的图片中被划红线圈住的代码是添加上去的,最终运行出错了,请问怎么修改,或者重新帮我写一个权重损失代码代码,跪谢 def custom_loss_4(y_true, y_pred, weights): return K.mean(K.abs(y_true - y_pred) * weights) model = models.Sequential() model.add(layers.Dense(200, activation = "relu", input_shape = (1175, ))) weights = np.random.randn(*X_train.shape) weights_tensor = Input(shape=(3,)) cl4 = partial(custom_loss_4,weights=weights_tensor) model.add(layers.Dropout(0.7)) model.add(layers.Dense(100, activation = "relu")) model.add(layers.Dropout(0.5)) model.add(layers.Dense(3, activation = "softmax")) model.compile(optimizer = "rmsprop", loss = cl4, metrics = ["accuracy"]) model.summary() ![图片说明](https://img-ask.csdn.net/upload/201910/13/1570937131_951272.jpg) ![图片说明](https://img-ask.csdn.net/upload/201910/13/1570936461_762097.png) ![图片说明](https://img-ask.csdn.net/upload/201910/13/1570936605_599703.png) 补充一下:我在前面对数据做过了不平衡调整,定义的函数如下: def calc_class_weight(total_y): my_class_weight = class_weight.compute_class_weight("balanced", np.unique(total_y), total_y) return my_class_weight
tensorflow 报错You must feed a value for placeholder tensor 'Placeholder_1' with dtype float and shape [?,32,32,3],但是怎么看数据都没错,请大神指点
调试googlenet的代码,总是报错 InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder_1' with dtype float and shape [?,32,32,3],但是我怎么看喂的数据都没问题,请大神们指点 ``` # -*- coding: utf-8 -*- """ GoogleNet也被称为InceptionNet Created on Mon Feb 10 12:15:35 2020 @author: 月光下的云海 """ import tensorflow as tf from keras.datasets import cifar10 import numpy as np import tensorflow.contrib.slim as slim tf.reset_default_graph() tf.reset_default_graph() (x_train,y_train),(x_test,y_test) = cifar10.load_data() x_train = x_train.astype('float32') x_test = x_test.astype('float32') y_train = y_train.astype('int32') y_test = y_test.astype('int32') y_train = y_train.reshape(y_train.shape[0]) y_test = y_test.reshape(y_test.shape[0]) x_train = x_train/255 x_test = x_test/255 #************************************************ 构建inception ************************************************ #构建一个多分支的网络结构 #INPUTS: # d0_1:最左边的分支,分支0,大小为1*1的卷积核个数 # d1_1:左数第二个分支,分支1,大小为1*1的卷积核的个数 # d1_3:左数第二个分支,分支1,大小为3*3的卷积核的个数 # d2_1:左数第三个分支,分支2,大小为1*1的卷积核的个数 # d2_5:左数第三个分支,分支2,大小为5*5的卷积核的个数 # d3_1:左数第四个分支,分支3,大小为1*1的卷积核的个数 # scope:参数域名称 # reuse:是否重复使用 #*************************************************************************************************************** def inception(x,d0_1,d1_1,d1_3,d2_1,d2_5,d3_1,scope = 'inception',reuse = None): with tf.variable_scope(scope,reuse = reuse): #slim.conv2d,slim.max_pool2d的默认参数都放在了slim的参数域里面 with slim.arg_scope([slim.conv2d,slim.max_pool2d],stride = 1,padding = 'SAME'): #第一个分支 with tf.variable_scope('branch0'): branch_0 = slim.conv2d(x,d0_1,[1,1],scope = 'conv_1x1') #第二个分支 with tf.variable_scope('branch1'): branch_1 = slim.conv2d(x,d1_1,[1,1],scope = 'conv_1x1') branch_1 = slim.conv2d(branch_1,d1_3,[3,3],scope = 'conv_3x3') #第三个分支 with tf.variable_scope('branch2'): branch_2 = slim.conv2d(x,d2_1,[1,1],scope = 'conv_1x1') branch_2 = slim.conv2d(branch_2,d2_5,[5,5],scope = 'conv_5x5') #第四个分支 with tf.variable_scope('branch3'): branch_3 = slim.max_pool2d(x,[3,3],scope = 'max_pool') branch_3 = slim.conv2d(branch_3,d3_1,[1,1],scope = 'conv_1x1') #连接 net = tf.concat([branch_0,branch_1,branch_2,branch_3],axis = -1) return net #*************************************** 使用inception构建GoogleNet ********************************************* #使用inception构建GoogleNet #INPUTS: # inputs-----------输入 # num_classes------输出类别数目 # is_trainning-----batch_norm层是否使用训练模式,batch_norm和is_trainning密切相关 # 当is_trainning = True 时候,它使用一个batch数据的平均移动,方差值 # 当is_trainning = Flase时候,它就使用固定的值 # verbos-----------控制打印信息 # reuse------------是否重复使用 #*************************************************************************************************************** def googlenet(inputs,num_classes,reuse = None,is_trainning = None,verbose = False): with slim.arg_scope([slim.batch_norm],is_training = is_trainning): with slim.arg_scope([slim.conv2d,slim.max_pool2d,slim.avg_pool2d], padding = 'SAME',stride = 1): net = inputs #googlnet的第一个块 with tf.variable_scope('block1',reuse = reuse): net = slim.conv2d(net,64,[5,5],stride = 2,scope = 'conv_5x5') if verbose: print('block1 output:{}'.format(net.shape)) #googlenet的第二个块 with tf.variable_scope('block2',reuse = reuse): net = slim.conv2d(net,64,[1,1],scope = 'conv_1x1') net = slim.conv2d(net,192,[3,3],scope = 'conv_3x3') net = slim.max_pool2d(net,[3,3],stride = 2,scope = 'max_pool') if verbose: print('block2 output:{}'.format(net.shape)) #googlenet第三个块 with tf.variable_scope('block3',reuse = reuse): net = inception(net,64,96,128,16,32,32,scope = 'inception_1') net = inception(net,128,128,192,32,96,64,scope = 'inception_2') net = slim.max_pool2d(net,[3,3],stride = 2,scope = 'max_pool') if verbose: print('block3 output:{}'.format(net.shape)) #googlenet第四个块 with tf.variable_scope('block4',reuse = reuse): net = inception(net,192,96,208,16,48,64,scope = 'inception_1') net = inception(net,160,112,224,24,64,64,scope = 'inception_2') net = inception(net,128,128,256,24,64,64,scope = 'inception_3') net = inception(net,112,144,288,24,64,64,scope = 'inception_4') net = inception(net,256,160,320,32,128,128,scope = 'inception_5') net = slim.max_pool2d(net,[3,3],stride = 2,scope = 'max_pool') if verbose: print('block4 output:{}'.format(net.shape)) #googlenet第五个块 with tf.variable_scope('block5',reuse = reuse): net = inception(net,256,160,320,32,128,128,scope = 'inception1') net = inception(net,384,182,384,48,128,128,scope = 'inception2') net = slim.avg_pool2d(net,[2,2],stride = 2,scope = 'avg_pool') if verbose: print('block5 output:{}'.format(net.shape)) #最后一块 with tf.variable_scope('classification',reuse = reuse): net = slim.flatten(net) net = slim.fully_connected(net,num_classes,activation_fn = None,normalizer_fn = None,scope = 'logit') if verbose: print('classification output:{}'.format(net.shape)) return net #给卷积层设置默认的激活函数和batch_norm with slim.arg_scope([slim.conv2d],activation_fn = tf.nn.relu,normalizer_fn = slim.batch_norm) as sc: conv_scope = sc is_trainning_ph = tf.placeholder(tf.bool,name = 'is_trainning') #定义占位符 x_train_ph = tf.placeholder(shape = (None,x_train.shape[1],x_train.shape[2],x_train.shape[3]),dtype = tf.float32) x_test_ph = tf.placeholder(shape = (None,x_test.shape[1],x_test.shape[2],x_test.shape[3]),dtype = tf.float32) y_train_ph = tf.placeholder(shape = (None,),dtype = tf.int32) y_test_ph = tf.placeholder(shape = (None,),dtype = tf.int32) #实例化网络 with slim.arg_scope(conv_scope): train_out = googlenet(x_train_ph,10,is_trainning = is_trainning_ph,verbose = True) val_out = googlenet(x_test_ph,10,is_trainning = is_trainning_ph,reuse = True) #定义loss和acc with tf.variable_scope('loss'): train_loss = tf.losses.sparse_softmax_cross_entropy(labels = y_train_ph,logits = train_out,scope = 'train') val_loss = tf.losses.sparse_softmax_cross_entropy(labels = y_test_ph,logits = val_out,scope = 'val') with tf.name_scope('accurcay'): train_acc = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(train_out,axis = -1,output_type = tf.int32),y_train_ph),tf.float32)) val_acc = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(val_out,axis = -1,output_type = tf.int32),y_test_ph),tf.float32)) #定义训练op lr = 1e-2 opt = tf.train.MomentumOptimizer(lr,momentum = 0.9) #通过tf.get_collection获得所有需要更新的op update_op = tf.get_collection(tf.GraphKeys.UPDATE_OPS) #使用tesorflow控制流,先执行update_op再进行loss最小化 with tf.control_dependencies(update_op): train_op = opt.minimize(train_loss) #开启会话 sess = tf.Session() saver = tf.train.Saver() sess.run(tf.global_variables_initializer()) batch_size = 64 #开始训练 for e in range(10000): batch1 = np.random.randint(0,50000,size = batch_size) t_x_train = x_train[batch1][:][:][:] t_y_train = y_train[batch1] batch2 = np.random.randint(0,10000,size = batch_size) t_x_test = x_test[batch2][:][:][:] t_y_test = y_test[batch2] sess.run(train_op,feed_dict = {x_train_ph:t_x_train, is_trainning_ph:True, y_train_ph:t_y_train}) # if(e%1000 == 999): # loss_train,acc_train = sess.run([train_loss,train_acc], # feed_dict = {x_train_ph:t_x_train, # is_trainning_ph:True, # y_train_ph:t_y_train}) # loss_test,acc_test = sess.run([val_loss,val_acc], # feed_dict = {x_test_ph:t_x_test, # is_trainning_ph:False, # y_test_ph:t_y_test}) # print('STEP{}:train_loss:{:.6f} train_acc:{:.6f} test_loss:{:.6f} test_acc:{:.6f}' # .format(e+1,loss_train,acc_train,loss_test,acc_test)) saver.save(sess = sess,save_path = 'VGGModel\model.ckpt') print('Train Done!!') print('--'*60) sess.close() ``` 报错信息是 ``` Using TensorFlow backend. block1 output:(?, 16, 16, 64) block2 output:(?, 8, 8, 192) block3 output:(?, 4, 4, 480) block4 output:(?, 2, 2, 832) block5 output:(?, 1, 1, 1024) classification output:(?, 10) Traceback (most recent call last): File "<ipython-input-1-6385a760fe16>", line 1, in <module> runfile('F:/Project/TEMP/LearnTF/GoogleNet/GoogleNet.py', wdir='F:/Project/TEMP/LearnTF/GoogleNet') File "D:\ANACONDA\Anaconda3\envs\spyder\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile execfile(filename, namespace) File "D:\ANACONDA\Anaconda3\envs\spyder\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "F:/Project/TEMP/LearnTF/GoogleNet/GoogleNet.py", line 177, in <module> y_train_ph:t_y_train}) File "D:\ANACONDA\Anaconda3\envs\spyder\lib\site-packages\tensorflow\python\client\session.py", line 900, in run run_metadata_ptr) File "D:\ANACONDA\Anaconda3\envs\spyder\lib\site-packages\tensorflow\python\client\session.py", line 1135, in _run feed_dict_tensor, options, run_metadata) File "D:\ANACONDA\Anaconda3\envs\spyder\lib\site-packages\tensorflow\python\client\session.py", line 1316, in _do_run run_metadata) File "D:\ANACONDA\Anaconda3\envs\spyder\lib\site-packages\tensorflow\python\client\session.py", line 1335, in _do_call raise type(e)(node_def, op, message) InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_1' with dtype float and shape [?,32,32,3] [[Node: Placeholder_1 = Placeholder[dtype=DT_FLOAT, shape=[?,32,32,3], _device="/job:localhost/replica:0/task:0/device:GPU:0"]()]] [[Node: gradients/block4/inception_4/concat_grad/ShapeN/_45 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_23694_gradients/block4/inception_4/concat_grad/ShapeN", tensor_type=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]] ``` 看了好多遍都不是喂数据的问题,百度说是summary出了问题,可是我也没有summary呀,头晕~~~~
求mnist多数字识别,修改完成我的代码
实现多个 数字的识别 如 ![图片说明](https://img-ask.csdn.net/upload/201906/10/1560141462_156564.png) 实现方法: 把五个数字的图 拼成一个 再进行 训练 和 测试 学校讲的自己看的都一知半解,我都不知道 我一个大二的,没什么基础的学生是怎么选上做这个研究的。。马上due就快到了,求大神在我代码基础上帮我完成。。 ``` ``` import tensorflow as tf import numpy as np from numpy import array import os import cv2 import matplotlib.pyplot as plt mnist = tf.keras.datasets.mnist (x_train, y_train), (x_test, y_test) = mnist.load_data() # x_train = tf.keras.utils.normalize(x_train, axis=1) # x_test = tf.keras.utils.normalize(x_test, axis=1) import random countain = 0 myxtrain = [] myytrain = [] while countain < 5: a_1 = random.randint(0, 10000) a_2 = random.randint(0, 10000) a_3 = random.randint(0, 10000) a_4 = random.randint(0, 10000) a_5 = random.randint(0, 10000) a = np.concatenate((x_train[a_1], x_train[a_2], x_train[a_3], x_train[a_4], x_train[a_5]), axis=1) myxtrain.append(a) labelx = [] s1 = str(y_train[a_1]) s2 = str(y_train[a_2]) s3 = str(y_train[a_3]) s4 = str(y_train[a_4]) s5 = str(y_train[a_5]) labelx.append(s1) labelx.append(s2) labelx.append(s3) labelx.append(s4) labelx.append(s5) s = ' '.join(labelx) print('----', s) # cv2.imwrite(os.path.join(path1, s + '.jpg'), a) # cv2.waitKey(0) myytrain.append(s) countain += 1 x_train = array(myxtrain) y_train = array(myytrain) from keras.layers import Dense, Dropout, Flatten from keras.layers import Conv2D, MaxPooling2D from keras.optimizers import SGD model = tf.keras.models.Sequential() model.add(Conv2D(5, (3, 3), activation='relu', input_shape=(28, 140, 2))) model.add(Conv2D(5, (3, 3), activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Flatten()) model.add(Dense(256, activation='relu')) model.add(Dense(10, activation='softmax')) sgd = SGD(lr=0.0001, decay=1e-6, momentum=0.9, nesterov=True) model.compile(loss='categorical_crossentropy', optimizer=sgd) model.fit(x_train, y_train, batch_size=1, epochs=10) ``` ```
optim.compute_gradients计算梯度 ,为什么返回的第一列为None?
1.问题描述 模型通过keras.models.Sequential构建 loss => tf.losses.sparse_softmax_cross_entropy 通过 var_list=tf.trainable_variables() 获取可训练变量 计算梯度值: loss_op = tf.losses.sparse_softmax_cross_entropy(y, y_pred) grads_vars = optim.compute_gradients(loss_op, tf.trainable_variables()) grads_vars返回的第一元素为 None,导致面的程序异常。 为什么grads_vars第一列返回的元素为None? 2.相关代码 ``` import tensorflow as tf import numpy as np import time import keras # 加载数据集 x_dataset=np.random.rand(1000,28,28,1) y_dataset=np.random.randint(0,10,size=(1000,)) act = tf.nn.leaky_relu epoch = 200 batch_size = 5000 n_batch = len(x_dataset) // batch_size # 把 batch 分成多少个 sub batch 来计算 subdivisions = 50 subdivisions_batch_size = int(np.ceil(batch_size / subdivisions)) # 是否使用 sub batch 方法,设置为 False 代表使用默认方法 is_on_subdivisions = True def get_model(is_train=True, reuse=False): with tf.variable_scope('model', reuse=reuse): net = keras.models.Sequential() net.add(keras.layers.Conv2D(128,(3,3),input_shape=(28,28,1),strides=(2,2),padding='same',name='c1')) net.add(keras.layers.GlobalAveragePooling2D()) net.add(keras.layers.Dense(10)) return net x = tf.placeholder(tf.float32, [None, 28, 28, 1]) y = tf.placeholder(tf.int32, [None,]) net = get_model() y_pred=tf.cast(tf.argmax(net.outputs[0],axis=-1),dtype=tf.float32) loss_op = tf.losses.sparse_softmax_cross_entropy(y, y_pred) optim = tf.train.AdamOptimizer(0.01) var_list=tf.trainable_variables() grads_vars = optim.compute_gradients(loss_op, tf.trainable_variables()) #grads_vars返回的第一列为None,为什么? for gv in grads_vars: print(gv) # 删掉没梯度的参数, 倒序删除,减少麻烦 for i in range(len(grads_vars))[::-1]: if grads_vars[i][0] is None: del grads_vars[i] #因为返回的第一列为None,所以所有变量都被删除了,导致后面的异常! print('len(grads_vars):',len(grads_vars)) # 生成梯度缓存:grads_vars第一列为None触发异常 grads_cache = [tf.Variable(np.zeros(t[0].shape.as_list(), np.float32), trainable=False) for t in grads_vars] # 清空梯度缓存op,每一 batch 开始前调用 clear_grads_cache_op = tf.group([gc.assign(tf.zeros_like(gc)) for gc in grads_cache]) # 累积梯度op,累积每个 sub batch 的梯度 accumulate_grad_op = tf.group([gc.assign_add(gv[0]) for gc, gv in zip(grads_cache, grads_vars)]) # 求平均梯度, mean_grad = [gc/tf.to_float(subdivisions) for gc in grads_cache] # 组装梯度列表 new_grads_vars = [(g, gv[1]) for g, gv in zip(mean_grad, grads_vars)] # 应用梯度op,累积完所有 sub batch 的梯度后,应用梯度 apply_grad_op = optim.apply_gradients(new_grads_vars) # 原来的 optim ,跟上面做对照 ori_optim_op = tf.train.AdamOptimizer(0.01).minimize(loss_op, var_list=net.all_params) config = tf.ConfigProto() config.gpu_options.allow_growth = True config.allow_soft_placement = True sess = tf.Session(config=config) sess.run(tf.global_variables_initializer()) for e in range(epoch): loss_sum = 0 for b in progressbar(range(n_batch)): x_batch = x_dataset[b * batch_size: (b + 1) * batch_size] y_batch = y_dataset[b * batch_size: (b + 1) * batch_size] if is_on_subdivisions: # 每一批开始前需要清空梯度缓存 sess.run(clear_grads_cache_op) sub_loss_sum = 0 for s in range(subdivisions): x_sub_batch = x_batch[s * subdivisions_batch_size: (s + 1) * subdivisions_batch_size] y_sub_batch = y_batch[s * subdivisions_batch_size: (s + 1) * subdivisions_batch_size] if len(x_sub_batch) == 0: break feed_dict = {x: x_sub_batch, y: y_sub_batch} _, los = sess.run([accumulate_grad_op, loss_op], feed_dict) sub_loss_sum += los loss_sum += sub_loss_sum / subdivisions # 梯度累积完成,开始应用梯度 sess.run(apply_grad_op) # 本批次结束 else: feed_dict = {x: x_batch, y: y_batch} _, los = sess.run([ori_optim_op, loss_op], feed_dict) loss_sum += los time.sleep(0.2) print('loss', loss_sum / n_batch) ``` 3.报错信息 ``` grads_vars: (None, <tf.Variable 'model/c1/kernel:0' shape=(3, 3, 1, 128) dtype=float32_ref>) (None, <tf.Variable 'model/c1/bias:0' shape=(128,) dtype=float32_ref>) (None, <tf.Variable 'model/dense_1/kernel:0' shape=(128, 10) dtype=float32_ref>) (None, <tf.Variable 'model/dense_1/bias:0' shape=(10,) dtype=float32_ref>) (None, <tf.Variable 'model_1/c1/kernel:0' shape=(3, 3, 1, 128) dtype=float32_ref>) (None, <tf.Variable 'model_1/c1/bias:0' shape=(128,) dtype=float32_ref>) (None, <tf.Variable 'model_1/dense_2/kernel:0' shape=(128, 10) dtype=float32_ref>) (None, <tf.Variable 'model_1/dense_2/bias:0' shape=(10,) dtype=float32_ref>) (None, <tf.Variable 'model_2/c1/kernel:0' shape=(3, 3, 1, 128) dtype=float32_ref>) (None, <tf.Variable 'model_2/c1/bias:0' shape=(128,) dtype=float32_ref>) (None, <tf.Variable 'model_2/dense_3/kernel:0' shape=(128, 10) dtype=float32_ref>) (None, <tf.Variable 'model_2/dense_3/bias:0' shape=(10,) dtype=float32_ref>) len(grads_vars): 0 ``` 4.尝试过的方法方式 5.相关截图
关于Tensorflow的DNN分类器
用Tensorflow写了一个简易的DNN网络(输入,一个隐层,输出),用作分类,数据集选用的是UCI 的iris数据集 激活函数使用softmax loss函数使用对数似然 以便最后的结果是一个概率解,选概率最大的分类的结果 目前的问题是预测结果出现问题,用测试数据测试显示结果如下 ![图片说明](https://img-ask.csdn.net/upload/201811/27/1543322274_512329.png) 刚刚入门...希望大家指点一下,谢谢辣! ``` #coding:utf-8 import matplotlib.pyplot as plt import tensorflow as tf import numpy as np import pandas as pd from sklearn.model_selection import train_test_split from sklearn.decomposition import PCA from sklearn import preprocessing from sklearn.model_selection import cross_val_score BATCH_SIZE = 30 iris = pd.read_csv('F:\dataset\iris\dataset.data', sep=',', header=None) ''' # 查看导入的数据 print("Dataset Lenght:: ", len(iris)) print("Dataset Shape:: ", iris.shape) print("Dataset:: ") print(iris.head(150)) ''' #将每条数据划分为样本值和标签值 X = iris.values[:, 0:4] Y = iris.values[:, 4] # 整理一下标签数据 # Iris-setosa ---> 0 # Iris-versicolor ---> 1 # Iris-virginica ---> 2 for i in range(len(Y)): if Y[i] == 'Iris-setosa': Y[i] = 0 elif Y[i] == 'Iris-versicolor': Y[i] = 1 elif Y[i] == 'Iris-virginica': Y[i] = 2 # 划分训练集与测试集 X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.3, random_state=10) #对数据集X与Y进行shape整理,让第一个参数为-1表示整理X成n行2列,整理Y成n行1列 X_train = np.vstack(X_train).reshape(-1, 4) Y_train = np.vstack(Y_train).reshape(-1, 1) X_test = np.vstack(X_test).reshape(-1, 4) Y_test = np.vstack(Y_test).reshape(-1, 1) ''' print(X_train) print(Y_train) print(X_test) print(Y_test) ''' #定义神经网络的输入,参数和输出,定义前向传播过程 def get_weight(shape): w = tf.Variable(tf.random_normal(shape), dtype=tf.float32) return w def get_bias(shape): b = tf.Variable(tf.constant(0.01, shape=shape)) return b x = tf.placeholder(tf.float32, shape=(None, 4)) yi = tf.placeholder(tf.float32, shape=(None, 1)) def BP_Model(): w1 = get_weight([4, 10]) # 第一个隐藏层,10个神经元,4个输入 b1 = get_bias([10]) y1 = tf.nn.softmax(tf.matmul(x, w1) + b1) # 注意维度 w2 = get_weight([10, 3]) # 输出层,3个神经元,10个输入 b2 = get_bias([3]) y = tf.nn.softmax(tf.matmul(y1, w2) + b2) return y def train(): # 生成计算图 y = BP_Model() # 定义损失函数 ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.arg_max(yi, 1)) loss_cem = tf.reduce_mean(ce) # 定义反向传播方法,正则化 train_step = tf.train.AdamOptimizer(0.001).minimize(loss_cem) # 定义保存器 saver = tf.train.Saver(tf.global_variables()) #生成会话 with tf.Session() as sess: init_op = tf.global_variables_initializer() sess.run(init_op) Steps = 5000 for i in range(Steps): start = (i * BATCH_SIZE) % 300 end = start + BATCH_SIZE reslut = sess.run(train_step, feed_dict={x: X_train[start:end], yi: Y_train[start:end]}) if i % 100 == 0: loss_val = sess.run(loss_cem, feed_dict={x: X_train, yi: Y_train}) print("step: ", i, "loss: ", loss_val) print("保存模型: ", saver.save(sess, './model_iris/bp_model.model')) tf.summary.FileWriter("logs/", sess.graph) #train() def prediction(): # 生成计算图 y = BP_Model() # 定义损失函数 ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.arg_max(yi, 1)) loss_cem = tf.reduce_mean(ce) # 定义保存器 saver = tf.train.Saver(tf.global_variables()) with tf.Session() as sess: saver.restore(sess, './model_iris/bp_model.model') result = sess.run(y, feed_dict={x: X_test}) loss_val = sess.run(loss_cem, feed_dict={x: X_test, yi: Y_test}) print("result :", result) print("loss :", loss_val) result_set = sess.run(tf.argmax(result, axis=1)) print("predict result: ", result_set) print("real result: ", Y_test.reshape(1, -1)) #prediction() ```
《MySQL 性能优化》之理解 MySQL 体系结构
本文介绍 MySQL 的体系结构,包括物理结构、逻辑结构以及插件式存储引擎。
程序员请照顾好自己,周末病魔差点一套带走我。
程序员在一个周末的时间,得了重病,差点当场去世,还好及时挽救回来了。
卸载 x 雷某度!GitHub 标星 1.5w+,从此我只用这款全能高速下载工具!
作者 | Rocky0429 来源 | Python空间 大家好,我是 Rocky0429,一个喜欢在网上收集各种资源的蒟蒻… 网上资源眼花缭乱,下载的方式也同样千奇百怪,比如 BT 下载,磁力链接,网盘资源等等等等,下个资源可真不容易,不一样的方式要用不同的下载软件,因此某比较有名的 x 雷和某度网盘成了我经常使用的工具。 作为一个没有钱的穷鬼,某度网盘几十 kb 的下载速度让我...
讲真,这两个IDE插件,可以让你写出质量杠杠的代码
周末躺在床上看《拯救大兵瑞恩》 周末在闲逛的时候,发现了两个优秀的 IDE 插件,据说可以提高代码的质量,我就安装了一下,试了试以后发现,确实很不错,就推荐给大家。 01、Alibaba Java 代码规范插件 《阿里巴巴 Java 开发手册》,相信大家都不会感到陌生,其 IDEA 插件的下载次数据说达到了 80 万次,我今天又贡献了一次。嘿嘿。 该项目的插件地址: https://github....
为什么猝死的都是程序员,基本上不见产品经理猝死呢?
相信大家时不时听到程序员猝死的消息,但是基本上听不到产品经理猝死的消息,这是为什么呢? 我们先百度搜一下:程序员猝死,出现将近700多万条搜索结果: 搜索一下:产品经理猝死,只有400万条的搜索结果,从搜索结果数量上来看,程序员猝死的搜索结果就比产品经理猝死的搜索结果高了一倍,而且从下图可以看到,首页里面的五条搜索结果,其实只有两条才是符合条件。 所以程序员猝死的概率真的比产品经理大,并不是错...
害怕面试被问HashMap?这一篇就搞定了!
声明:本文以jdk1.8为主! 搞定HashMap 作为一个Java从业者,面试的时候肯定会被问到过HashMap,因为对于HashMap来说,可以说是Java集合中的精髓了,如果你觉得自己对它掌握的还不够好,我想今天这篇文章会非常适合你,至少,看了今天这篇文章,以后不怕面试被问HashMap了 其实在我学习HashMap的过程中,我个人觉得HashMap还是挺复杂的,如果真的想把它搞得明明白...
毕业5年,我问遍了身边的大佬,总结了他们的学习方法
我问了身边10个大佬,总结了他们的学习方法,原来成功都是有迹可循的。
python爬取百部电影数据,我分析出了一个残酷的真相
2019年就这么匆匆过去了,就在前几天国家电影局发布了2019年中国电影市场数据,数据显示去年总票房为642.66亿元,同比增长5.4%;国产电影总票房411.75亿元,同比增长8.65%,市场占比 64.07%;城市院线观影人次17.27亿,同比增长0.64%。 看上去似乎是一片大好对不对?不过作为一名严谨求实的数据分析师,我从官方数据中看出了一点端倪:国产票房增幅都已经高达8.65%了,为什...
推荐10个堪称神器的学习网站
每天都会收到很多读者的私信,问我:“二哥,有什么推荐的学习网站吗?最近很浮躁,手头的一些网站都看烦了,想看看二哥这里有什么新鲜货。” 今天一早做了个恶梦,梦到被老板辞退了。虽然说在我们公司,只有我辞退老板的份,没有老板辞退我这一说,但是还是被吓得 4 点多都起来了。(主要是因为我掌握着公司所有的核心源码,哈哈哈) 既然 4 点多起来,就得好好利用起来。于是我就挑选了 10 个堪称神器的学习网站,推...
这些软件太强了,Windows必装!尤其程序员!
Windows可谓是大多数人的生产力工具,集娱乐办公于一体,虽然在程序员这个群体中都说苹果是信仰,但是大部分不都是从Windows过来的,而且现在依然有很多的程序员用Windows。 所以,今天我就把我私藏的Windows必装的软件分享给大家,如果有一个你没有用过甚至没有听过,那你就赚了????,这可都是提升你幸福感的高效率生产力工具哦! 走起!???? NO、1 ScreenToGif 屏幕,摄像头和白板...
阿里面试,面试官没想到一个ArrayList,我都能跟他扯半小时
我是真的没想到,面试官会这样问我ArrayList。
曾经优秀的人,怎么就突然不优秀了。
职场上有很多辛酸事,很多合伙人出局的故事,很多技术骨干被裁员的故事。说来模板都类似,曾经是名校毕业,曾经是优秀员工,曾经被领导表扬,曾经业绩突出,然而突然有一天,因为种种原因,被裁员了,...
C语言荣获2019年度最佳编程语言
关注、星标公众号,不错过精彩内容作者:黄工公众号:strongerHuang近日,TIOBE官方发布了2020年1月编程语言排行榜单。我在前面给过一篇文章《2019年11月C语言接近Ja...
大学四年因为知道了这32个网站,我成了别人眼中的大神!
依稀记得,毕业那天,我们导员发给我毕业证的时候对我说“你可是咱们系的风云人物啊”,哎呀,别提当时多开心啦????,嗯,我们导员是所有导员中最帅的一个,真的???? 不过,导员说的是实话,很多人都叫我大神的,为啥,因为我知道这32个网站啊,你说强不强????,这次是绝对的干货,看好啦,走起来! PS:每个网站都是学计算机混互联网必须知道的,真的牛杯,我就不过多介绍了,大家自行探索,觉得没用的,尽管留言吐槽吧???? 社...
良心推荐,我珍藏的一些Chrome插件
上次搬家的时候,发了一个朋友圈,附带的照片中不小心暴露了自己的 Chrome 浏览器插件之多,于是就有小伙伴评论说分享一下我觉得还不错的浏览器插件。 我下面就把我日常工作和学习中经常用到的一些 Chrome 浏览器插件分享给大家,随便一个都能提高你的“生活品质”和工作效率。 Markdown Here Markdown Here 可以让你更愉快的写邮件,由于支持 Markdown 直接转电子邮...
看完这篇HTTP,跟面试官扯皮就没问题了
我是一名程序员,我的主要编程语言是 Java,我更是一名 Web 开发人员,所以我必须要了解 HTTP,所以本篇文章就来带你从 HTTP 入门到进阶,看完让你有一种恍然大悟、醍醐灌顶的感觉。 最初在有网络之前,我们的电脑都是单机的,单机系统是孤立的,我还记得 05 年前那会儿家里有个电脑,想打电脑游戏还得两个人在一个电脑上玩儿,及其不方便。我就想为什么家里人不让上网,我的同学 xxx 家里有网,每...
应届生/社招面试最爱问的几道Java基础问题
本文已经收录自笔者开源的 JavaGuide: https://github.com/Snailclimb (【Java学习 面试指南】 一份涵盖大部分Java程序员所需要掌握的核心知识)如果觉得不错的还,不妨去点个Star,鼓励一下! 一 为什么 Java 中只有值传递? 首先回顾一下在程序设计语言中有关将参数传递给方法(或函数)的一些专业术语。按值调用(call by value)表...
史上最全的IDEA快捷键总结
现在Idea成了主流开发工具,这篇博客对其使用的快捷键做了总结,希望对大家的开发工作有所帮助。
阿里程序员写了一个新手都写不出的低级bug,被骂惨了。
这种新手都不会范的错,居然被一个工作好几年的小伙子写出来,差点被当场开除了。
谁是华为扫地僧?
是的,华为也有扫地僧!2020年2月11-12日,“养在深闺人不知”的华为2012实验室扫地僧们,将在华为开发者大会2020(Cloud)上,和大家见面。到时,你可以和扫地僧们,吃一个洋...
Idea 中最常用的10款插件(提高开发效率),一定要学会使用!
学习使用一些插件,可以提高开发效率。对于我们开发人员很有帮助。这篇博客介绍了开发中使用的插件。
AI 没让人类失业,搞 AI 的人先失业了
最近和几个 AI 领域的大佬闲聊 根据他们讲的消息和段子 改编出下面这个故事 如有雷同 都是巧合 1. 老王创业失败,被限制高消费 “这里写我跑路的消息实在太夸张了。” 王葱葱哼笑一下,把消息分享给群里。 阿杰也看了消息,笑了笑。在座几位也都笑了。 王葱葱是个有名的人物,21岁那年以全额奖学金进入 KMU 攻读人工智能博士,累计发表论文 40 余篇,个人技术博客更是成为深度学习领域内风向标。 ...
2020年,冯唐49岁:我给20、30岁IT职场年轻人的建议
点击“技术领导力”关注∆每天早上8:30推送 作者|Mr.K 编辑| Emma 来源|技术领导力(ID:jishulingdaoli) 前天的推文《冯唐:职场人35岁以后,方法论比经验重要》,收到了不少读者的反馈,觉得挺受启发。其实,冯唐写了不少关于职场方面的文章,都挺不错的。可惜大家只记住了“春风十里不如你”、“如何避免成为油腻腻的中年人”等不那么正经的文章。 本文整理了冯...
最全最强!世界大学计算机专业排名总结!
我正在参与CSDN200进20,希望得到您的支持,扫码续投票5次。感谢您! (为表示感谢,您投票后私信我,我把我总结的人工智能手推笔记和思维导图发送给您,感谢!) 目录 泰晤士高等教育世界大学排名 QS 世界大学排名 US News 世界大学排名 世界大学学术排名(Academic Ranking of World Universities) 泰晤士高等教育世界大学排名 中国共...
一份王者荣耀的英雄数据报告
咪哥杂谈本篇阅读时间约为 6 分钟。1前言前一阵写了关于王者的一些系列文章,从数据的获取到数据清洗,数据落地,都是为了本篇的铺垫。今天来实现一下,看看不同维度得到的结论。2环境准备本次实...
作为一名大学生,如何在B站上快乐的学习?
B站是个宝,谁用谁知道???? 作为一名大学生,你必须掌握的一项能力就是自学能力,很多看起来很牛X的人,你可以了解下,人家私底下一定是花大量的时间自学的,你可能会说,我也想学习啊,可是嘞,该学习啥嘞,不怕告诉你,互联网时代,最不缺的就是学习资源,最宝贵的是啥? 你可能会说是时间,不,不是时间,而是你的注意力,懂了吧! 那么,你说学习资源多,我咋不知道,那今天我就告诉你一个你必须知道的学习的地方,人称...
那些年,我们信了课本里的那些鬼话
教材永远都是有错误的,从小学到大学,我们不断的学习了很多错误知识。 斑羚飞渡 在我们学习的很多小学课文里,有很多是错误文章,或者说是假课文。像《斑羚飞渡》: 随着镰刀头羊的那声吼叫,整个斑羚群迅速分成两拨,老年斑羚为一拨,年轻斑羚为一拨。 就在这时,我看见,从那拨老斑羚里走出一只公斑羚来。公斑羚朝那拨年轻斑羚示意性地咩了一声,一只半大的斑羚应声走了出来。一老一少走到伤心崖,后退了几步,突...
一个程序在计算机中是如何运行的?超级干货!!!
强烈声明:本文很干,请自备茶水!???? 开门见山,咱不说废话! 你有没有想过,你写的程序,是如何在计算机中运行的吗?比如我们搞Java的,肯定写过这段代码 public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } ...
【蘑菇街技术部年会】程序员与女神共舞,鼻血再次没止住。(文末内推)
蘑菇街技术部的年会,别开生面,一样全是美女。
那个在阿里养猪的工程师,5年了……
简介: 在阿里,走过1825天,没有趴下,依旧斗志满满,被称为“五年陈”。他们会被授予一枚戒指,过程就叫做“授戒仪式”。今天,咱们听听阿里的那些“五年陈”们的故事。 下一个五年,猪圈见! 我就是那个在养猪场里敲代码的工程师,一年多前我和20位工程师去了四川的猪场,出发前总架构师慷慨激昂的说:同学们,中国的养猪产业将因为我们而改变。但到了猪场,发现根本不是那么回事:要个WIFI,没有;...
立即提问