Tensorflow 的tf.one_hot()功能:dense to one hot

import tensorflow as tf
indices = [[3], [5], [0], [7]]
indices = tf.concat(0, indices)
indices = tf.reshape(indice, (4, 1))
a = tf.one_hot(indices, depth=10, on_value=None, off_value=None, axis=None, dtype=None, name=None)
print ("a is : ")
print a
b = tf.reshape(a, (4, 10))
print ("a is : ")
print b

'''
a is : 
Tensor("one_hot:0", shape=(4, 1, 10), dtype=float32)
a is : 
Tensor("Reshape_1:0", shape=(4, 10), dtype=float32)
'''
Args:
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
column_name = ["label"] column_name.extend(["pixel%d" % i for i in range(32 * 32 * 3)]) dataset = pd.read_csv('cifar_train.csv') #dataset = pd.read_csv('heart.csv') #dataset = pd.read_csv('iris.csuv') #sns.pairplot(dataset.iloc[:, 1:6]) #plt.show() #print(dataset.head()) #shuffled_data = dataset.sample(frac=1) #dataset=shuffled_data #index=[0,1,2,3,4,5,6,7,8,9,10,11,12,13] #dataset.columns=index dataset2=pd.read_csv('test.csv') #X = dataset.iloc[:, :30].values #y = dataset.iloc[:,30].values mm = MinMaxScaler() from sklearn.model_selection import train_test_split #X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=0) X_train =dataset.iloc[:,1:].values X_test = dataset2.iloc[:,1:].values y_train = dataset.iloc[:,0].values y_test = dataset2.iloc[:,0].values print(y_train) # 进行独热编码 def one_hot_encode_object_array(arr): # 去重获取全部的类别 uniques, ids = np.unique(arr, return_inverse=True) # 返回热编码的结果 return tf.keras.utils.to_categorical(ids, len(uniques)) #train_y_ohe=y_train #test_y_ohe=y_test # 训练集热编码 train_y_ohe = one_hot_encode_object_array(y_train) # 测试集热编码 test_y_ohe = one_hot_encode_object_array(y_test) # 利用sequential方式构建模型 from keras import backend as K def swish(x, beta=1.0): return x * K.sigmoid(beta * x) from keras import regularizers model = tf.keras.models.Sequential([ # 隐藏层1,激活函数是relu,输入大小有input_shape指定 tf.keras.layers.InputLayer(input_shape=(3072,)), # lambda(hanshu, output_shape=None, mask=None, arguments=None), #tf.keras.layers.Lambda(hanshu, output_shape=None, mask=None, arguments=None), tf.keras.layers.Dense(500, activation="relu"), # 隐藏层2,激活函数是relu tf.keras.layers.Dense(500, activation="relu"), # 输出层 tf.keras.layers.Dense(10, activation="softmax") ])
最新发布
07-15
你的代码中存在一些问题: 1. 你导入的库 `Keras` 和 `backend` 应该是 `tensorflow.keras` 和 `tensorflow.keras.backend`。以你需要将代码中的 `ker` 和 `backend` 替换为 `tensorflow` 和 `tensorflow.keras.backend`。 2. 你在模型中使用的激活函数 `swish` 并没有定义。你可以在使用之前定义一个 `swish` 函数,可以参考以下示例: ```python def swish(x, beta=1.0): return x * tf.sigmoid(beta * x) ``` 3. 在创建模型时,你使用了 `tf.keras.models.Sequential`,但是你导入的是 `keras` 库而不是 `tensorflow.keras` 库。所以你需要将代码中的 `keras.models.Sequential` 替换为 `tensorflow.keras.models.Sequential`。 4. 在模型的第一层之前,你可以添加一个 `tf.keras.layers.InputLayer` 来指定输入的形状。你可以将其添加到模型定义的开头,如下所示: ```python model = tf.keras.models.Sequential([ tf.keras.layers.InputLayer(input_shape=(3072,)), ... ]) ``` 根据上述修改,你的模型定义应该如下所示: ```python import tensorflow as tf def swish(x, beta=1.0): return x * tf.sigmoid(beta * x) model = tf.keras.models.Sequential([ tf.keras.layers.InputLayer(input_shape=(3072,)), tf.keras.layers.Dense(500, activation="relu"), tf.keras.layers.Dense(500, activation="relu"), tf.keras.layers.Dense(10, activation="softmax") ]) ``` 请注意,我只修改了与模型定义相关的部分。如果你的代码中还有其他问题,你需要继续检查并做出相应的修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值