keras

低阶API示范

一笑奈何 提交于 2021-02-04 21:03:31
TensorFlow有5个不同的层次结构: 即 硬件层 , 内核层 , 低阶API , 中阶API , 高阶API 。 本章我们将以线性回归为例,直观对比展示在低阶API,中阶API,高阶API这三个层级实现模型的特点。 TensorFlow的层次结构从低到高可以分成如下五层。 最底层为硬件层,TensorFlow支持CPU、GPU或TPU加入计算资源池。 第二层为C++实现的内核,kernel可以跨平台分布运行。 第三层为Python实现的操作符,提供了封装C++内核的低级API指令,主要包括各种张量操作算子、计算图、自动微分. 如tf.Variable,tf.constant,tf.function,tf.GradientTape,tf.nn.softmax... 如果把模型比作一个房子,那么第三层API就是【模型之砖】。 第四层为Python实现的模型组件,对低级API进行了函数封装,主要包括各种模型层,损失函数,优化器,数据管道,特征列等等。 如tf.keras.layers,tf.keras.losses,tf.keras.metrics,tf.keras.optimizers,tf.data.Dataset,tf.feature_column... 如果把模型比作一个房子,那么第四层API就是【模型之墙】。 第五层为Python实现的模型成品

Derivative in loss function in Keras

廉价感情. 提交于 2021-02-04 19:38:48
问题 I want to make following loss function in keras: Loss = mse + double_derivative(y_pred,x_train) I am not able to incorporate the derivative term. I have tried K.gradients(K.gradients(y_pred,x_train),x_train) but it does not help. I am getting error message: AttributeError: 'NoneType' object has no attribute 'op' def _loss_tensor(y_true, y_pred,x_train): l1 = K.mean(K.square(y_true - y_pred), axis=-1) sigma = 0.01 lamda = 3 term = K.square(sigma)*K.gradients(K.gradients(y_pred,x_train),x_train

Set half of the filters of a layer as not trainable keras/tensorflow

放肆的年华 提交于 2021-02-04 14:56:22
问题 I'm trying to train a model suggested by this research paper where I set half of the filters of a convolution layer to Gabor filters and the rest are random weights which are initialized by default. Normally, if I have to set a layer as not trainable, I set the trainable attribute as False . But here I have to freeze only half of the filters of a layer and I have no idea how to do so. Any help would be really appreciated. I'm using Keras with Tensorflow backend. 回答1: How about making two

Set half of the filters of a layer as not trainable keras/tensorflow

坚强是说给别人听的谎言 提交于 2021-02-04 14:55:08
问题 I'm trying to train a model suggested by this research paper where I set half of the filters of a convolution layer to Gabor filters and the rest are random weights which are initialized by default. Normally, if I have to set a layer as not trainable, I set the trainable attribute as False . But here I have to freeze only half of the filters of a layer and I have no idea how to do so. Any help would be really appreciated. I'm using Keras with Tensorflow backend. 回答1: How about making two

How do I use Tensorflow tf.nn.conv2 to make a convolutional layer?

别来无恙 提交于 2021-02-04 08:35:07
问题 With tf.nn.conv2d , you can perform a convolutional operation on a tensor. E.g. import tensorflow as tf x = tf.random.uniform(shape=(1, 224, 224, 3), dtype=tf.float32) filters = tf.random.uniform(shape=(1, 3, 3, 10)) tf.nn.conv2d(input=x, filters=filters, strides=1, padding='VALID') <tf.Tensor: shape=(1, 224, 222, 10), dtype=float32, numpy= array([[[[2.1705112, 1.2065555, 1.7674012, ..., 1.705754 , 1.3659815, 1.7028458], [2.0048866, 1.4835871, 1.2038497, ..., 1.8981357, 1.4605963, 2.148876 ],

AttributeError: 'NoneType' object has no attribute 'image_data_format' in keras resnet50

吃可爱长大的小学妹 提交于 2021-02-04 06:34:09
问题 I am trying to use Resnet50 model for training. from keras import backend as K from keras_applications.resnet50 import ResNet50 from keras.layers import Input from keras.callbacks import ModelCheckpoint K.set_image_data_format('channels_last') K.set_image_dim_ordering('tf') input_layer = Input(shape=(224, 224, 3)) model = ResNet50(include_top=True, weights=None, classes=2) model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy']) Why is the following error showing

AttributeError: 'NoneType' object has no attribute 'image_data_format' in keras resnet50

主宰稳场 提交于 2021-02-04 06:32:24
问题 I am trying to use Resnet50 model for training. from keras import backend as K from keras_applications.resnet50 import ResNet50 from keras.layers import Input from keras.callbacks import ModelCheckpoint K.set_image_data_format('channels_last') K.set_image_dim_ordering('tf') input_layer = Input(shape=(224, 224, 3)) model = ResNet50(include_top=True, weights=None, classes=2) model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy']) Why is the following error showing

TypeError: The added layer must be an instance of class Layer. Found: <keras.engine.training.Model object at 0x7fa5bee17ac8>

家住魔仙堡 提交于 2021-02-02 09:56:45
问题 I am try to train a model using Xception /Inception Model of keras library but I face value error Dataset which I use it from kaggle commuinity and Notebook which I refer Notebook But I am try to use different Model like Xception /Inception but silmilar idea not work for me with strategy.scope(): enet = keras.applications.inception_v3.InceptionV3( input_shape=(512, 512, 3), weights='imagenet', include_top=False ) model = tf.keras.Sequential([ enet, tf.keras.layers.GlobalAveragePooling2D(), tf

Keras returns binary results

天涯浪子 提交于 2021-02-02 09:56:41
问题 I want to predict the kind of 2 diseases but I get results as binary (like 1.0 and 0.0). How can I get accuracy of these (like 0.7213)? Training code: from keras.models import Sequential from keras.layers import Conv2D from keras.layers import MaxPooling2D from keras.layers import Flatten from keras.layers import Dense # Intialising the CNN classifier = Sequential() # Step 1 - Convolution classifier.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation = 'relu')) # Step 2 - Pooling

How to balance dataset using fit_generator() in Keras?

匆匆过客 提交于 2021-02-02 09:23:57
问题 I am trying to use keras to fit a CNN model to classify 2 classes of data . I have imbalanced dataset I want to balance the data. I don't know can I use class_weight in model.fit_generator . I wonder if I used class_weight="balanced" in model.fit_generator The main code : def generate_arrays_for_training(indexPat, paths, start=0, end=100): while True: from_=int(len(paths)/100*start) to_=int(len(paths)/100*end) for i in range(from_, int(to_)): f=paths[i] x = np.load(PathSpectogramFolder+f) x =