我已经实施了MultiAttention head在Transformers. 周围有太多的实现,所以很混乱。有人可以验证我的实施是否正确:
DotProductAttention 引用自:https://www.tensorflow.org/tutorials/text/transformer#setup
import tensorflow as tf
def scaled_dot_product(q,k,v):
#calculates Q . K(transpose)
qkt = tf.matmul(q,k,transpose_b=True)
#caculates scaling factor
dk = tf.math.sqrt(tf.cast(q.shape[-1],dtype=tf.float32))
scaled_qkt = qkt/dk
softmax = tf.nn.softmax(scaled_qkt,axis=-1)
z = tf.matmul(softmax,v)
#shape: (m,Tx,depth), same shape as q,k,v
return z
class MultiAttention(tf.keras.layers.Layer):
def __init__(self,d_model,num_of_heads):
super(MultiAttention,self).__init__()
self.d_model = d_model
self.num_of_heads = num_of_heads
self.depth = d_model//num_of_heads
self.wq = [tf.keras.layers.Dense(self.depth) for i in range(num_of_heads)]
self.wk = [tf.keras.layers.Dense(self.depth) for i in range(num_of_heads)]
self.wv = [tf.keras.layers.Dense(self.depth) for i in …Run Code Online (Sandbox Code Playgroud) 架构:
|-- c0: string (nullable = true)
|-- c1: struct (nullable = true)
| |-- c2: array (nullable = true)
| | |-- element: struct (containsNull = true)
| | | |-- orangeID: string (nullable = true)
| | | |-- orangeId: string (nullable = true)
Run Code Online (Sandbox Code Playgroud)
我试图在火花中展平上面的架构。
码:
var df = data.select($"c0",$"c1.*").select($"c0",explode($"c2")).select($"c0",$"col.orangeID", $"col.orangeId")
Run Code Online (Sandbox Code Playgroud)
拼合代码工作正常。问题出在最后一部分,其中两列的区别仅在于1个字母(orangeID和orangeId)。因此,我收到此错误:
错误:
org.apache.spark.sql.AnalysisException: Ambiguous reference to fields StructField(orangeID,StringType,true), StructField(orangeId,StringType,true);
Run Code Online (Sandbox Code Playgroud)
任何避免这种歧义的建议都是很好的。
我正在尝试找到一种下载模型en_core_web_lg ==2.3.1的方法Spacy == 2.3.2。
目前使用
python -m spacy download en_core_web_lg
import spacy
nlp = spacy.load ("en_core_web_lg")
Run Code Online (Sandbox Code Playgroud)
model file or directory是否可以直接从下载的文件夹中下载load the model。
我正在尝试在 scikit learn 中绘制逻辑回归的决策边界
features_train_df : 650 columns, 5250 rows
features_test_df : 650 columns, 1750 rows
class_train_df = 1 column (class to be predicted), 5250 rows
class_test_df = 1 column (class to be predicted), 1750 rows
Run Code Online (Sandbox Code Playgroud)
分类器代码;
tuned_logreg = LogisticRegression(penalty = 'l2', tol = 0.0001,C = 0.1,max_iter = 100,class_weight = "balanced")
tuned_logreg.fit(x_train[sorted_important_features_list[0:650]].values, y_train['loss'].values)
y_pred_3 = tuned_logreg.predict(x_test[sorted_important_features_list[0:650]].values)
Run Code Online (Sandbox Code Playgroud)
我得到了分类器代码的正确输出。
在网上得到这个代码:
code:
X = features_train_df.values
# evenly sampled points
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
y_min, y_max = …Run Code Online (Sandbox Code Playgroud) 我正在尝试将新列添加到我的spark DF。我了解可以使用以下代码:
df.withColumn("row",monotonically_increasing_id)
Run Code Online (Sandbox Code Playgroud)
但是我的用例是:
输入DF:
col value
1
2
2
3
3
3
Run Code Online (Sandbox Code Playgroud)
输出DF:
col_value identifier
1 1
2 1
2 2
3 1
3 2
3 3
Run Code Online (Sandbox Code Playgroud)
关于使用monotonically_increasing或rowWithUniqueIndex进行获取的任何建议。
apache-spark ×2
nlp ×2
scala ×2
keras ×1
lstm ×1
matplotlib ×1
python ×1
scikit-learn ×1
spacy ×1
spacy-3 ×1
tensorflow ×1