搭建一个简易网络模型:
import tensorflow as tf model=tf.keras.Sequential() model.add(tf.keras.layers.Dense(64,input_shape=(20,),activation='relu')) model.add(tf.keras.layers.Dense(32,activation='relu')) model.add(tf.keras.layers.Dense(5,activation='softmax')) model.summary()
#方法二
import tensorflow as tf model=tf.keras.Sequential([ tf.keras.layers.Dense(64,input_shape=(20,),activation='relu'), tf.keras.layers.Dense(32,activation='relu'), tf.keras.layers.Dense(5,activation='softmax') ]) model.summary()
简单的神经网络实例
import tensorflow as tf import numpy as np x_train=np.random.random((10000,15)) y_train=tf.keras.utils.to_categorical(np.random.randint(10,size=(10000,1)),num_classes=10) x_test=np.random.random((10000,15)) y_test=tf.keras.utils.to_categorical(np.random.randint(10,size=(10000,1)),num_classes=10) model=tf.keras.Sequential([ tf.keras.layers.Dense(512,activation=tf.nn.relu), tf.keras.layers.Dropout(0.5), tf.keras.layers.Dense(128,activation=tf.nn.relu), tf.keras.layers.Dropout(0.5), tf.keras.layers.Dense(10,activation=tf.nn.softmax) ]) model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'] ) model.fit(x_train,y_train,epochs=5,batch_size=128)
——2019.11.12