欢迎来到 TensorFlow 代码社区,这里汇集了丰富的 TensorFlow 相关代码资源,帮助你更好地学习和应用 TensorFlow。
最新代码分享
以下是一些最新的 TensorFlow 代码分享,希望对您有所帮助:
图像分类:使用 TensorFlow 实现一个简单的图像分类器。
import tensorflow as tf # 构建模型 model = tf.keras.models.Sequential([ tf.keras.layers.Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)), tf.keras.layers.MaxPooling2D(pool_size=(2, 2)), tf.keras.layers.Flatten(), tf.keras.layers.Dense(128, activation='relu'), tf.keras.layers.Dense(10, activation='softmax') ]) # 编译模型 model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) # 训练模型 model.fit(x_train, y_train, epochs=10, batch_size=32)
自然语言处理:使用 TensorFlow 实现一个简单的文本分类器。
import tensorflow as tf from tensorflow.keras.preprocessing.text import Tokenizer from tensorflow.keras.preprocessing.sequence import pad_sequences # 数据预处理 tokenizer = Tokenizer(num_words=1000) tokenizer.fit_on_texts(texts) sequences = tokenizer.texts_to_sequences(texts) padded_sequences = pad_sequences(sequences, maxlen=100) # 构建模型 model = tf.keras.models.Sequential([ tf.keras.layers.Embedding(1000, 16, input_length=100), tf.keras.layers.GlobalAveragePooling1D(), tf.keras.layers.Dense(24, activation='relu'), tf.keras.layers.Dense(1, activation='sigmoid') ]) # 编译模型 model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) # 训练模型 model.fit(padded_sequences, labels, epochs=10)
扩展阅读
如果您想了解更多关于 TensorFlow 代码的知识,可以访问以下链接:
社区交流
加入 TensorFlow 社区,与其他开发者一起学习和分享:
TensorFlow Logo