activity_regularizer=l1_regularizer(0.01)在tensorflow2怎么改
时间: 2023-06-13 21:07:33 浏览: 54
在TensorFlow 2中,使用`tf.keras.regularizers`模块来设置正则化器。要使用L1正则化器,可以使用`tf.keras.regularizers.l1`,并将其作为参数传递给相应的层。例如,如果要在全连接层中应用L1正则化器,则可以按照以下方式编写代码:
```
from tensorflow.keras.layers import Dense
from tensorflow.keras.regularizers import l1
model = tf.keras.Sequential([
Dense(64, activation='relu', kernel_regularizer=l1(0.01)),
Dense(10, activation='softmax')
])
```
在这个例子中,我们在第一层中应用了一个L1正则化器,其强度为0.01。您可以根据需要调整正则化器的强度。
相关问题
代码:import os import pandas as pd folder_path = "D:/活动生成" file_name = "活动.xlsx" new_file_name = "一口价3.xlsx" activity_file_path = os.path.join(folder_path, file_name) activity_df = pd.read_excel(activity_file_path) discounts = {"6.9折": 0.69, "5折": 0.5, "3.9折": 0.39} new_price_df = pd.DataFrame(columns=["商品编码", "价格"]) for index, row in activity_df.iterrows(): if row["活动"] in discounts: standard_price = row["标准售价"] discount = discounts.get(row["活动"], 1) new_price = round(standard_price * discount, 2) # only save the 7-digit 商品编码 product_code = str(row["商品编码"]) if len(product_code) == 7: new_price_df.loc[index] = [product_code, new_price] new_file_path = os.path.join(folder_path, new_file_name) new_price_df.to_excel(new_file_path, index=False) print("已生成") 没法保存商品编码为0开头的商品编码,例如商品编码为:0311069的商品编码不能保存,我需要将0开头的商品编码能进行计算的同时也保存下来
可以将商品编码转换为字符串后,判断字符串长度是否为7位,如果不足7位,在前面添加相应数量的0,使其长度为7位。修改后的代码如下:
```
import os
import pandas as pd
folder_path = "D:/活动生成"
file_name = "活动.xlsx"
new_file_name = "一口价3.xlsx"
activity_file_path = os.path.join(folder_path, file_name)
activity_df = pd.read_excel(activity_file_path)
discounts = {"6.9折": 0.69, "5折": 0.5, "3.9折": 0.39}
new_price_df = pd.DataFrame(columns=["商品编码", "价格"])
for index, row in activity_df.iterrows():
if row["活动"] in discounts:
standard_price = row["标准售价"]
discount = discounts.get(row["活动"], 1)
new_price = round(standard_price * discount, 2)
# convert product code to 7 digits
product_code = str(row["商品编码"]).zfill(7)
new_price_df.loc[index] = [product_code, new_price]
new_file_path = os.path.join(folder_path, new_file_name)
new_price_df.to_excel(new_file_path, index=False)
print("已生成")
```
这样就可以保存商品编码为0开头的商品编码了。
代码:import os import pandas as pd print("开始计算一口价") folder_path = "D:/一口价生成" file_name = "活动.xlsx" new_file_name = "一口价及升价3.xlsx" activity_file_path = os.path.join(folder_path, file_name) activity_df = pd.read_excel(activity_file_path) discounts = {"6.9折": 0.69, "5折": 0.5, "3.9折": 0.39} new_price_df = pd.DataFrame(columns=["商品编码", "价格"]) for index, row in activity_df.iterrows(): if row["活动"] in discounts: standard_price = row["标准售价"] discount = discounts.get(row["活动"], 1) new_price = round(standard_price * discount, 2) product_code = str(row["商品编码"]).zfill(7) new_price_df.loc[index] = [product_code, new_price] elif isinstance(row["活动"], int) or isinstance(row["活动"], float): product_code = str(row["商品编码"]).zfill(7) new_price = int(row["活动"]) new_price_df.loc[index] = [product_code, new_price] new_file_path = os.path.join(folder_path, new_file_name) new_price_df.to_excel(new_file_path, index=False) new_file_path = os.path.join(folder_path, new_file_name) new_price_df.to_excel(new_file_path, index=False) print("已生成") 将“活动”列中所有数值都以int类型输出了,请修改代码输出类型位:如果是int类型,则输出int类型。是float类型,就输出float类型。,
代码:
import os
import pandas as pd
print("开始计算一口价")
folder_path = "D:/一口价生成"
file_name = "活动.xlsx"
new_file_name = "一口价及升价3.xlsx"
activity_file_path = os.path.join(folder_path, file_name)
activity_df = pd.read_excel(activity_file_path)
discounts = {"6.9折": 0.69, "5折": 0.5, "3.9折": 0.39}
new_price_df = pd.DataFrame(columns=["商品编码", "价格"])
for index, row in activity_df.iterrows():
if row["活动"] in discounts:
standard_price = row["标准售价"]
discount = discounts.get(row["活动"], 1)
new_price = round(standard_price * discount, 2)
product_code = str(row["商品编码"]).zfill(7)
new_price_df.loc[index] = [product_code, new_price]
elif isinstance(row["活动"], int):
product_code = str(row["商品编码"]).zfill(7)
new_price = int(row["活动"])
new_price_df.loc[index] = [product_code, new_price]
elif isinstance(row["活动"], float):
product_code = str(row["商品编码"]).zfill(7)
new_price = float(row["活动"])
new_price_df.loc[index] = [product_code, new_price]
new_file_path = os.path.join(folder_path, new_file_name)
new_price_df.to_excel(new_file_path, index=False)
print("已生成")
修改后的代码中,增加了判断“活动”列中是否为float类型的判断条件,并针对不同类型输出对应类型的数据。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)