简体   繁体   English

具有不同数据类型的神经网络层

[英]Neural Network layers with different data type

I would like to make a neural network whose layers consists of different data types.我想制作一个神经网络,其层由不同的数据类型组成。 for example, one layer could be of INT16 data type and one layer could be of Float16 or INT32.例如,一层可能是 INT16 数据类型,一层可能是 Float16 或 INT32。

I am trying to make it using the dtype keyword.我正在尝试使用 dtype 关键字来实现它。 I am able to achieve this for different float data types.我能够为不同的浮点数据类型实现这一点。 ie float16 and float32 using the following code snippet.即 float16 和 float32 使用以下代码片段。 but when i tried to do the same for INT32 and Float, it gave me an error.但是当我尝试对 INT32 和 Float 做同样的事情时,它给了我一个错误。 I am not able to figure it out.我无法弄清楚。

TypeError: Unable to build Dense layer with non-floating point dtype <dtype: 'int16'> TypeError:无法使用非浮点 dtype <dtype: 'int16'> 构建Dense

Could anyone help me out to make such a network with different datatypes谁能帮我制作一个具有不同数据类型的网络


# Code Snippet 
from tensorflow.keras.models import Model
i = Input(shape= x_train[0].shape, dtype=tf.float16)
x = Flatten(dtype=tf.float16)(i)
x = Dense(64, activation='relu',dtype=tf.float32)(x)
x = Dense(64, activation='relu')(x)
x = Dense(64, activation='relu', dtype=tf.float16)(x)
x = Dense(64, activation='relu', dtype=tf.float16)(x)
x = Dense(10, activation='softmax')(x)

model = Model(i, x)

# data type of model's layer can be observed as 

input_14  :  float16
flatten_13  :  float16
dense_57  :  float32
dense_58  :  float32
dense_59  :  float16
dense_60  :  float16
dense_61  :  float32

The code which gave error给出错误的代码

# Step 4- Now let's make the ANN!
from tensorflow.keras.models import Model
i = Input(shape= x_train[0].shape, dtype=tf.int16)
x = Flatten(dtype=tf.int16)(i)
x = Dense(64, activation='relu',dtype=tf.int16)(x)        # error occurred at this line
x = Dense(64, activation='relu')(x)
x = Dense(64, activation='relu', dtype=tf.float16)(x)
x = Dense(64, activation='relu', dtype=tf.float16)(x)
x = Dense(10, activation='softmax')(x)

This is not something you can do just by casting to tf.int32 .这不是你可以通过转换为tf.int32来完成的。 What you're talking about is called model quantization and is an extremely complicated process.你说的叫model量化,是一个极其复杂的过程。 Tensorflow, via Tensorflow Lite, offers many resources for quantization aware training. Tensorflow,通过 Tensorflow Lite,为量化感知训练提供了许多资源 I suggest you start there.我建议你从那里开始。

Quantization aware training emulates inference-time quantization, creating a model that downstream tools will use to produce actually quantized models.量化感知训练模拟推理时间量化,创建一个 model,下游工具将使用它来生成实际量化的模型。 The quantized models use lower-precision (eg 8-bit instead of 32-bit float), leading to benefits during deployment.量化模型使用较低精度(例如 8 位而不是 32 位浮点数),从而在部署期间带来好处。

With this example you can train a MNIST model using integers:通过此示例,您可以使用整数训练 MNIST model:

import tempfile
import os
import tensorflow as tf
from tensorflow import keras

mnist = keras.datasets.mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

train_images = train_images / 255.0
test_images = test_images / 255.0

model = keras.Sequential([
  keras.layers.InputLayer(input_shape=(28, 28)),
  keras.layers.Reshape(target_shape=(28, 28, 1)),
  keras.layers.Conv2D(filters=12, kernel_size=(3, 3), activation='relu'),
  keras.layers.MaxPooling2D(pool_size=(2, 2)),
  keras.layers.Flatten(),
  keras.layers.Dense(10)
])

# Train the digit classification model
model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

model.fit(
  train_images,
  train_labels,
  epochs=1,
  validation_split=0.1,
)
import tensorflow_model_optimization as tfmot

quantize_model = tfmot.quantization.keras.quantize_model

# q_aware stands for for quantization aware.
q_aware_model = quantize_model(model)

# `quantize_model` requires a recompile.
q_aware_model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

train_images_subset = train_images[0:1000] # out of 60000
train_labels_subset = train_labels[0:1000]

q_aware_model.fit(train_images_subset, train_labels_subset,
                  batch_size=500, epochs=1, validation_split=0.1)

_, baseline_model_accuracy = model.evaluate(
    test_images, test_labels, verbose=0)

_, q_aware_model_accuracy = q_aware_model.evaluate(
   test_images, test_labels, verbose=0)

print('Baseline test accuracy:', baseline_model_accuracy)
print('Quant test accuracy:', q_aware_model_accuracy)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM